From e242df76b883970b86ec4a732a4b0f3f13f049e4 Mon Sep 17 00:00:00 2001 From: fufesou Date: Mon, 27 Mar 2023 11:59:07 +0800 Subject: [PATCH 1/3] simplify keyboard lock modifiers Signed-off-by: fufesou --- src/keyboard.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/keyboard.rs b/src/keyboard.rs index 40de9b5df..aaa12afa0 100644 --- a/src/keyboard.rs +++ b/src/keyboard.rs @@ -374,14 +374,14 @@ fn is_numpad_key(event: &Event) -> bool { } #[cfg(not(any(target_os = "android", target_os = "ios")))] -fn parse_add_lock_modes_modifiers(key_event: &mut KeyEvent, lock_modes: i32, is_numpad_key: bool) { +fn parse_add_lock_modes_modifiers(key_event: &mut KeyEvent, lock_modes: i32) { const CAPS_LOCK: i32 = 1; const NUM_LOCK: i32 = 2; // const SCROLL_LOCK: i32 = 3; - if !is_numpad_key && (lock_modes & (1 << CAPS_LOCK) != 0) { + if lock_modes & (1 << CAPS_LOCK) != 0 { key_event.modifiers.push(ControlKey::CapsLock.into()); } - if is_numpad_key && (lock_modes & (1 << NUM_LOCK) != 0) { + if lock_modes & (1 << NUM_LOCK) != 0 { key_event.modifiers.push(ControlKey::NumLock.into()); } // if lock_modes & (1 << SCROLL_LOCK) != 0 { @@ -390,11 +390,11 @@ fn parse_add_lock_modes_modifiers(key_event: &mut KeyEvent, lock_modes: i32, is_ } #[cfg(not(any(target_os = "android", target_os = "ios")))] -fn add_lock_modes_modifiers(key_event: &mut KeyEvent, is_numpad_key: bool) { - if !is_numpad_key && get_key_state(enigo::Key::CapsLock) { +fn add_lock_modes_modifiers(key_event: &mut KeyEvent) { + if get_key_state(enigo::Key::CapsLock) { key_event.modifiers.push(ControlKey::CapsLock.into()); } - if is_numpad_key && get_key_state(enigo::Key::NumLock) { + if get_key_state(enigo::Key::NumLock) { key_event.modifiers.push(ControlKey::NumLock.into()); } } @@ -479,13 +479,12 @@ pub fn event_to_key_events( }; if keyboard_mode != KeyboardMode::Translate { - let is_numpad_key = is_numpad_key(&event); for key_event in &mut key_events { #[cfg(not(any(target_os = "android", target_os = "ios")))] if let Some(lock_modes) = lock_modes { - parse_add_lock_modes_modifiers(key_event, lock_modes, is_numpad_key); + parse_add_lock_modes_modifiers(key_event, lock_modes); } else { - add_lock_modes_modifiers(key_event, is_numpad_key); + add_lock_modes_modifiers(key_event); } } } From 17fe62aec18d0051f5207135f39bb670432df269 Mon Sep 17 00:00:00 2001 From: fufesou Date: Mon, 27 Mar 2023 12:06:38 +0800 Subject: [PATCH 2/3] Do not send lock modifiers if no related keys are pressed Signed-off-by: fufesou --- src/keyboard.rs | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/src/keyboard.rs b/src/keyboard.rs index aaa12afa0..003925e96 100644 --- a/src/keyboard.rs +++ b/src/keyboard.rs @@ -366,22 +366,37 @@ pub fn get_keyboard_mode_enum() -> KeyboardMode { #[inline] fn is_numpad_key(event: &Event) -> bool { matches!(event.event_type, EventType::KeyPress(key) | EventType::KeyRelease(key) if match key { - Key::Kp0 | Key::Kp1 | Key::Kp2 | Key::Kp3| Key::Kp4| Key::Kp5| Key::Kp6| - Key::Kp7| Key::Kp8| Key::Kp9 | Key::KpMinus | Key::KpMultiply | - Key::KpDivide | Key::KpPlus | Key::KpDecimal => true, + Key::Kp0 | Key::Kp1 | Key::Kp2 | Key::Kp3 | Key::Kp4| Key::Kp5| Key::Kp6| Key::Kp7 | Key::Kp8 | + Key::Kp9 | Key::KpMinus | Key::KpMultiply | Key::KpDivide | Key::KpPlus | Key::KpDecimal => true, + _ => false + }) +} + +#[inline] +fn is_letter_key(event: &Event) -> bool { + matches!(event.event_type, EventType::KeyPress(key) | EventType::KeyRelease(key) if match key { + Key::KeyA | Key::KeyB | Key::KeyC | Key::KeyD | Key::KeyE | Key::KeyF | Key::KeyG | Key::KeyH | + Key::KeyI | Key::KeyJ | Key::KeyK | Key::KeyL | Key::KeyM | Key::KeyN | Key::KeyO | Key::KeyP | + Key::KeyQ | Key::KeyR | Key::KeyS | Key::KeyT | Key::KeyU | Key::KeyV | Key::KeyW | Key::KeyX | + Key::KeyY | Key::KeyZ => true, _ => false }) } #[cfg(not(any(target_os = "android", target_os = "ios")))] -fn parse_add_lock_modes_modifiers(key_event: &mut KeyEvent, lock_modes: i32) { +fn parse_add_lock_modes_modifiers( + key_event: &mut KeyEvent, + lock_modes: i32, + is_numpad_key: bool, + is_letter_key: bool, +) { const CAPS_LOCK: i32 = 1; const NUM_LOCK: i32 = 2; // const SCROLL_LOCK: i32 = 3; - if lock_modes & (1 << CAPS_LOCK) != 0 { + if is_letter_key && (lock_modes & (1 << CAPS_LOCK) != 0) { key_event.modifiers.push(ControlKey::CapsLock.into()); } - if lock_modes & (1 << NUM_LOCK) != 0 { + if is_numpad_key && lock_modes & (1 << NUM_LOCK) != 0 { key_event.modifiers.push(ControlKey::NumLock.into()); } // if lock_modes & (1 << SCROLL_LOCK) != 0 { @@ -390,11 +405,11 @@ fn parse_add_lock_modes_modifiers(key_event: &mut KeyEvent, lock_modes: i32) { } #[cfg(not(any(target_os = "android", target_os = "ios")))] -fn add_lock_modes_modifiers(key_event: &mut KeyEvent) { - if get_key_state(enigo::Key::CapsLock) { +fn add_lock_modes_modifiers(key_event: &mut KeyEvent, is_numpad_key: bool, is_letter_key: bool) { + if is_letter_key && get_key_state(enigo::Key::CapsLock) { key_event.modifiers.push(ControlKey::CapsLock.into()); } - if get_key_state(enigo::Key::NumLock) { + if is_numpad_key && get_key_state(enigo::Key::NumLock) { key_event.modifiers.push(ControlKey::NumLock.into()); } } @@ -479,12 +494,14 @@ pub fn event_to_key_events( }; if keyboard_mode != KeyboardMode::Translate { + let is_numpad_key = is_numpad_key(&event); + let is_letter_key = is_letter_key(&event); for key_event in &mut key_events { #[cfg(not(any(target_os = "android", target_os = "ios")))] if let Some(lock_modes) = lock_modes { - parse_add_lock_modes_modifiers(key_event, lock_modes); + parse_add_lock_modes_modifiers(key_event, lock_modes, is_numpad_key, is_letter_key); } else { - add_lock_modes_modifiers(key_event); + add_lock_modes_modifiers(key_event, is_numpad_key, is_letter_key); } } } From 3fdffa1371f37d1efd0f53dd55e3feeb8d78e063 Mon Sep 17 00:00:00 2001 From: fufesou Date: Mon, 27 Mar 2023 12:08:01 +0800 Subject: [PATCH 3/3] format Signed-off-by: fufesou --- src/keyboard.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/keyboard.rs b/src/keyboard.rs index 003925e96..5a41d6c5c 100644 --- a/src/keyboard.rs +++ b/src/keyboard.rs @@ -366,7 +366,7 @@ pub fn get_keyboard_mode_enum() -> KeyboardMode { #[inline] fn is_numpad_key(event: &Event) -> bool { matches!(event.event_type, EventType::KeyPress(key) | EventType::KeyRelease(key) if match key { - Key::Kp0 | Key::Kp1 | Key::Kp2 | Key::Kp3 | Key::Kp4| Key::Kp5| Key::Kp6| Key::Kp7 | Key::Kp8 | + Key::Kp0 | Key::Kp1 | Key::Kp2 | Key::Kp3 | Key::Kp4 | Key::Kp5 | Key::Kp6 | Key::Kp7 | Key::Kp8 | Key::Kp9 | Key::KpMinus | Key::KpMultiply | Key::KpDivide | Key::KpPlus | Key::KpDecimal => true, _ => false })