Merge pull request #3796 from fufesou/refact/simplify_keyboard_lock_modifiers

simplify keyboard lock modifiers
This commit is contained in:
RustDesk 2023-03-27 14:56:51 +08:00 committed by GitHub
commit 7675be024f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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, is_numpad_key: bool) {
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 !is_numpad_key && (lock_modes & (1 << CAPS_LOCK) != 0) {
if is_letter_key && (lock_modes & (1 << CAPS_LOCK) != 0) {
key_event.modifiers.push(ControlKey::CapsLock.into());
}
if is_numpad_key && (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,8 +405,8 @@ 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, 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 is_numpad_key && get_key_state(enigo::Key::NumLock) {
@ -480,12 +495,13 @@ 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, is_numpad_key);
parse_add_lock_modes_modifiers(key_event, lock_modes, is_numpad_key, is_letter_key);
} else {
add_lock_modes_modifiers(key_event, is_numpad_key);
add_lock_modes_modifiers(key_event, is_numpad_key, is_letter_key);
}
}
}