From 21bc75bc31a3e9c6e78582023c99935a16e3a196 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Sun, 24 Oct 2021 21:46:09 +0200 Subject: [PATCH] fix(engine): prevent other keys from interfering with toggle_key. Fix #815 --- .../src/process/middleware/disable.rs | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/espanso-engine/src/process/middleware/disable.rs b/espanso-engine/src/process/middleware/disable.rs index 77bd0e8..5b7e633 100644 --- a/espanso-engine/src/process/middleware/disable.rs +++ b/espanso-engine/src/process/middleware/disable.rs @@ -64,18 +64,24 @@ impl Middleware for DisableMiddleware { match &event.etype { EventType::Keyboard(m_event) => { - if is_toggle_key(m_event, &self.options) { + if m_event.status == Status::Released { let mut last_toggle_press = self.last_toggle_press.borrow_mut(); - if let Some(previous_press) = *last_toggle_press { - if previous_press.elapsed() < self.options.toggle_key_maximum_window { - *enabled = !*enabled; - *last_toggle_press = None; - has_status_changed = true; + if is_toggle_key(m_event, &self.options) { + if let Some(previous_press) = *last_toggle_press { + if previous_press.elapsed() < self.options.toggle_key_maximum_window { + *enabled = !*enabled; + *last_toggle_press = None; + has_status_changed = true; + } else { + *last_toggle_press = Some(Instant::now()); + } } else { *last_toggle_press = Some(Instant::now()); } } else { - *last_toggle_press = Some(Instant::now()); + // If another key is pressed (not the toggle key), we should reset the window + // For more information, see: https://github.com/federico-terzi/espanso/issues/815 + *last_toggle_press = None; } } } @@ -115,10 +121,6 @@ impl Middleware for DisableMiddleware { } fn is_toggle_key(event: &KeyboardEvent, options: &DisableOptions) -> bool { - if event.status != Status::Released { - return false; - } - if options .toggle_key .as_ref()