From e0a7238cc1d5856cafcc0bc05c80e6ffc6cdcbd3 Mon Sep 17 00:00:00 2001 From: Asura Date: Mon, 18 Jul 2022 02:09:32 -0700 Subject: [PATCH] Refactor rdev simulate --- src/server/input_service.rs | 42 +++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/server/input_service.rs b/src/server/input_service.rs index eac11a743..2061c909d 100644 --- a/src/server/input_service.rs +++ b/src/server/input_service.rs @@ -579,21 +579,23 @@ pub fn handle_key(evt: &KeyEvent) { handle_key_(evt); } -fn rdev_simulate(event_type: &EventType) { +fn rdev_key_down_or_up(key: RdevKey, down_or_up: bool) { + let event_type = match down_or_up { + true => EventType::KeyPress(key), + false => EventType::KeyRelease(key), + }; let delay = std::time::Duration::from_millis(20); - match simulate(event_type) { + match simulate(&event_type) { Ok(()) => (), Err(_simulate_error) => { - // todo - log::error!("rdev could not send {:?}", event_type); + println!("We could not send {:?}", &event_type); } } // Let ths OS catchup (at least MacOS) std::thread::sleep(delay); } -fn map_keyboard_map(evt: &KeyEvent) { - // map mode(1): Send keycode according to the peer platform. +fn sync_status(evt: &KeyEvent) { let mut en = ENIGO.lock().unwrap(); // sync CAPS status let caps_locking = evt @@ -601,24 +603,24 @@ fn map_keyboard_map(evt: &KeyEvent) { .iter() .position(|&r| r == ControlKey::CapsLock.into()) .is_some(); - println!("[*] remote, client: {:?} {:?}", caps_locking, en.get_key_state(enigo::Key::CapsLock)); - if caps_locking && !en.get_key_state(enigo::Key::CapsLock) + println!( + "[*] remote, client: {:?} {:?}", + caps_locking, + en.get_key_state(enigo::Key::CapsLock) + ); + if (caps_locking && !en.get_key_state(enigo::Key::CapsLock)) + || (!caps_locking && en.get_key_state(enigo::Key::CapsLock)) { println!("[*]: Changing status"); - rdev_simulate(&EventType::KeyPress(RdevKey::CapsLock)); - rdev_simulate(&EventType::KeyRelease(RdevKey::CapsLock)); - }else if !caps_locking && en.get_key_state(enigo::Key::CapsLock){ - println!("[*]: Changing status"); - rdev_simulate(&EventType::KeyPress(RdevKey::CapsLock)); - rdev_simulate(&EventType::KeyRelease(RdevKey::CapsLock)); + rdev_key_down_or_up(RdevKey::CapsLock, true); + rdev_key_down_or_up(RdevKey::CapsLock, false); }; +} - let event_type = match evt.down { - true => EventType::KeyPress(RdevKey::Unknown(evt.get_chr())), - false => EventType::KeyRelease(RdevKey::Unknown(evt.get_chr())), - }; - - rdev_simulate(&event_type); +fn map_keyboard_map(evt: &KeyEvent) { + // map mode(1): Send keycode according to the peer platform. + sync_status(evt); + rdev_key_down_or_up(RdevKey::Unknown(evt.get_chr()), evt.down); return; }