Use map mode when keyboard monitor

This commit is contained in:
Asura 2022-07-12 00:33:20 -07:00
parent 0ae6620659
commit 9837c9b893

View File

@ -289,9 +289,83 @@ impl Handler {
std::thread::spawn(move || {
// This will block.
std::env::set_var("KEYBOARD_ONLY", "y"); // pass to rdev
let keyboard_mode = 1;
if keyboard_mode == 1 {
use rdev::{Event, EventType::*, Key as RdevKey};
lazy_static::lazy_static! {
static ref MUTEX_SPECIAL_KEYS: Mutex<HashMap<RdevKey, bool>> = {
let mut m = HashMap::new();
// m.insert(RdevKey::PrintScreen, false); // TODO
m.insert(RdevKey::ShiftLeft, false);
m.insert(RdevKey::ShiftRight, false);
m.insert(RdevKey::ControlLeft, false);
m.insert(RdevKey::ControlRight, false);
m.insert(RdevKey::Alt, false);
m.insert(RdevKey::AltGr, false);
Mutex::new(m)
};
}
// todo: auto change paltform
let func = move |evt: Event| {
if !IS_IN.load(Ordering::SeqCst)
|| !SERVER_KEYBOARD_ENABLED.load(Ordering::SeqCst)
{
return;
}
let (key, down) = match evt.event_type {
KeyPress(k) => {
// keyboard long press
if MUTEX_SPECIAL_KEYS.lock().unwrap().contains_key(&k) {
if *MUTEX_SPECIAL_KEYS.lock().unwrap().get(&k).unwrap() {
return;
}
MUTEX_SPECIAL_KEYS.lock().unwrap().insert(k, true);
}
println!("keydown {:?} {:?} {:?}", k, evt.code, evt.scan_code);
(k, 1)
}
KeyRelease(k) => {
// keyboard long press
if MUTEX_SPECIAL_KEYS.lock().unwrap().contains_key(&k) {
MUTEX_SPECIAL_KEYS.lock().unwrap().insert(k, false);
}
println!("keyup {:?} {:?} {:?}", k, evt.code, evt.scan_code);
(k, 0)
}
_ => return,
};
// todo: clear key
#[cfg(target_os = "windows")]
let key = rdev::get_win_key(evt.code.into(), evt.scan_code);
// todo: up down left right in numpad
// #[cfg(target_os = "linux")]
dbg!(key);
println!("--------------");
let mut key_event = KeyEvent::new();
// According to peer platform.
if peer == "linux" {
let keycode: u32 = rdev::linux_keycode_from_key(key).unwrap().into();
key_event.set_chr(keycode);
} else if peer == "Windows" {
let keycode: u32 = rdev::win_keycode_from_key(key).unwrap().into();
key_event.set_chr(keycode);
} else if peer == "Mac OS" {
let keycode: u32 = rdev::macos_keycode_from_key(key).unwrap().into();
key_event.set_chr(keycode);
}
me.key_down_or_up(down, key_event, false, false, false, false);
};
if let Err(error) = rdev::listen(func) {
log::error!("rdev: {:?}", error);
}
} else {
use rdev::{EventType::*, *};
let func = move |evt: Event| {
if !IS_IN.load(Ordering::SeqCst) || !SERVER_KEYBOARD_ENABLED.load(Ordering::SeqCst)
if !IS_IN.load(Ordering::SeqCst)
|| !SERVER_KEYBOARD_ENABLED.load(Ordering::SeqCst)
{
return;
}
@ -502,6 +576,7 @@ impl Handler {
if let Err(error) = rdev::listen(func) {
log::error!("rdev: {:?}", error);
}
};
});
}