feat(inject): wire up inject_delay on macOS. Fix #849

This commit is contained in:
Federico Terzi 2022-01-03 20:36:58 +01:00
parent 7588900c06
commit 697f51e210
3 changed files with 10 additions and 8 deletions

View File

@ -32,7 +32,7 @@ use crate::{keys, InjectionOptions, Injector};
#[allow(improper_ctypes)]
#[link(name = "espansoinject", kind = "static")]
extern "C" {
pub fn inject_string(string: *const c_char);
pub fn inject_string(string: *const c_char, delay: i32);
pub fn inject_separate_vkeys(vkey_array: *const i32, vkey_count: i32, delay: i32);
pub fn inject_vkeys_combination(vkey_array: *const i32, vkey_count: i32, delay: i32);
}
@ -60,10 +60,10 @@ impl MacInjector {
}
impl Injector for MacInjector {
fn send_string(&self, string: &str, _: InjectionOptions) -> Result<()> {
fn send_string(&self, string: &str, options: InjectionOptions) -> Result<()> {
let c_string = CString::new(string)?;
unsafe {
inject_string(c_string.as_ptr());
inject_string(c_string.as_ptr(), options.delay);
}
Ok(())
}

View File

@ -23,7 +23,7 @@
#include <stdint.h>
// Inject a complete string using the KEYEVENTF_UNICODE flag
extern "C" void inject_string(char * string);
extern "C" void inject_string(char * string, int32_t delay);
// Send a sequence of vkey presses and releases
extern "C" void inject_separate_vkeys(int32_t *vkey_array, int32_t vkey_count, int32_t delay);

View File

@ -27,8 +27,10 @@
// so that we can later skip them in the detect module.
CGPoint ESPANSO_POINT_MARKER = CGPointMake(-27469, 0);
void inject_string(char *string)
void inject_string(char *string, int32_t delay)
{
long udelay = delay * 1000;
char * stringCopy = strdup(string);
dispatch_async(dispatch_get_main_queue(), ^(void) {
// Convert the c string to a UniChar array as required by the CGEventKeyboardSetUnicodeString method
@ -49,7 +51,7 @@ void inject_string(char *string)
CGEventPost(kCGHIDEventTap, e2);
CFRelease(e2);
usleep(2000);
usleep(udelay);
}
// Because of a bug ( or undocumented limit ) of the CGEventKeyboardSetUnicodeString method
@ -69,7 +71,7 @@ void inject_string(char *string)
CGEventPost(kCGHIDEventTap, e);
CFRelease(e);
usleep(2000);
usleep(udelay);
// Some applications require an explicit release of the space key
// For more information: https://github.com/federico-terzi/espanso/issues/159
@ -78,7 +80,7 @@ void inject_string(char *string)
CGEventPost(kCGHIDEventTap, e2);
CFRelease(e2);
usleep(2000);
usleep(udelay);
i += chunk_size;
}