From d20eaf1abafeed1af320af727f40c9cb21e93b3e Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Mon, 6 Sep 2021 23:10:56 +0200 Subject: [PATCH] feat(inject): add evdev_modifier_delay option --- espanso-inject/src/evdev/mod.rs | 15 +++++++++++++++ espanso-inject/src/lib.rs | 5 +++++ 2 files changed, 20 insertions(+) diff --git a/espanso-inject/src/evdev/mod.rs b/espanso-inject/src/evdev/mod.rs index 23887d1..e04835d 100644 --- a/espanso-inject/src/evdev/mod.rs +++ b/espanso-inject/src/evdev/mod.rs @@ -249,6 +249,12 @@ impl EVDEVInjector { Ok(()) } } + + fn wait_delay(&self, delay_ms: u32) { + if delay_ms > 0 { + std::thread::sleep(std::time::Duration::from_millis(delay_ms as u64)); + } + } } impl Injector for EVDEVInjector { @@ -267,6 +273,7 @@ impl Injector for EVDEVInjector { .collect(); let delay_us = options.delay as u32 * 1000; // Convert to micro seconds + let modifier_delay_ms = options.evdev_modifier_delay; // We need to keep track of the modifiers currently pressed to // press or release them accordingly @@ -277,13 +284,21 @@ impl Injector for EVDEVInjector { // Release all the modifiers that are not needed anymore for expired_modifier in current_modifiers.difference(&record_modifiers) { + self.wait_delay(modifier_delay_ms); + self.send_key(*expired_modifier, false, delay_us); + + self.wait_delay(modifier_delay_ms); } // Press all the new modifiers that are now needed for new_modifier in record_modifiers.difference(¤t_modifiers) { + self.wait_delay(modifier_delay_ms); + self.wait_until_key_is_released(record.code)?; self.send_key(*new_modifier, true, delay_us); + + self.wait_delay(modifier_delay_ms); } // Send the char diff --git a/espanso-inject/src/lib.rs b/espanso-inject/src/lib.rs index 090970d..dfd5748 100644 --- a/espanso-inject/src/lib.rs +++ b/espanso-inject/src/lib.rs @@ -57,6 +57,10 @@ pub struct InjectionOptions { // using XSendEvent rather than XTestFakeKeyEvent // NOTE: Only relevant on X11 linux systems. pub disable_fast_inject: bool, + + // Used to set a modifier-specific delay. + // NOTE: Only relevant on Wayland systems. + pub evdev_modifier_delay: u32, } impl Default for InjectionOptions { @@ -78,6 +82,7 @@ impl Default for InjectionOptions { Self { delay: default_delay, disable_fast_inject: false, + evdev_modifier_delay: 10, } } }