Replace pynput with tfc
This commit is contained in:
parent
30bfa59e7d
commit
78112e9754
@ -9,7 +9,7 @@ use std::{
|
|||||||
sync::atomic::{AtomicBool, Ordering},
|
sync::atomic::{AtomicBool, Ordering},
|
||||||
time::Instant,
|
time::Instant,
|
||||||
};
|
};
|
||||||
use tfc::{traits::*, Context};
|
use tfc::{traits::*, Context as TFC_Context, Key as TFC_Key};
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct StateCursor {
|
struct StateCursor {
|
||||||
@ -180,7 +180,7 @@ lazy_static::lazy_static! {
|
|||||||
};
|
};
|
||||||
static ref KEYS_DOWN: Arc<Mutex<HashMap<u64, Instant>>> = Default::default();
|
static ref KEYS_DOWN: Arc<Mutex<HashMap<u64, Instant>>> = Default::default();
|
||||||
static ref LATEST_INPUT: Arc<Mutex<Input>> = Default::default();
|
static ref LATEST_INPUT: Arc<Mutex<Input>> = Default::default();
|
||||||
static ref KBD_CONTEXT: Mutex<Context> = Mutex::new(Context::new().expect("kbd context error"));
|
static ref TFC_CONTEXT: Mutex<TFC_Context> = Mutex::new(TFC_Context::new().expect("kbd context error"));
|
||||||
}
|
}
|
||||||
static EXITING: AtomicBool = AtomicBool::new(false);
|
static EXITING: AtomicBool = AtomicBool::new(false);
|
||||||
|
|
||||||
@ -668,12 +668,113 @@ fn map_keyboard_mode(evt: &KeyEvent) {
|
|||||||
if evt.down && click_capslock {
|
if evt.down && click_capslock {
|
||||||
rdev_key_down_or_up(RdevKey::CapsLock, evt.down);
|
rdev_key_down_or_up(RdevKey::CapsLock, evt.down);
|
||||||
}
|
}
|
||||||
log::info!("click capslog {:?} click_numlock {:?}", click_capslock, click_numlock);
|
log::info!(
|
||||||
|
"click capslog {:?} click_numlock {:?}",
|
||||||
|
click_capslock,
|
||||||
|
click_numlock
|
||||||
|
);
|
||||||
|
|
||||||
rdev_key_down_or_up(RdevKey::Unknown(evt.chr()), evt.down);
|
rdev_key_down_or_up(RdevKey::Unknown(evt.chr()), evt.down);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn tfc_key_down_or_up(key: Key, down: bool, up: bool) {
|
||||||
|
if let Key::Layout(chr) = key {
|
||||||
|
log::info!("tfc_key_down_or_up: {:?}", chr);
|
||||||
|
if down {
|
||||||
|
TFC_CONTEXT.lock().unwrap().unicode_char_down(chr);
|
||||||
|
}
|
||||||
|
if up {
|
||||||
|
TFC_CONTEXT.lock().unwrap().unicode_char_up(chr);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let key = match key {
|
||||||
|
Key::Alt => TFC_Key::Alt,
|
||||||
|
Key::Backspace => TFC_Key::DeleteOrBackspace,
|
||||||
|
Key::CapsLock => TFC_Key::CapsLock,
|
||||||
|
Key::Control => TFC_Key::Control,
|
||||||
|
Key::Delete => TFC_Key::ForwardDelete,
|
||||||
|
Key::DownArrow => TFC_Key::DownArrow,
|
||||||
|
Key::End => TFC_Key::End,
|
||||||
|
Key::Escape => TFC_Key::Escape,
|
||||||
|
Key::F1 => TFC_Key::F1,
|
||||||
|
Key::F10 => TFC_Key::F10,
|
||||||
|
Key::F11 => TFC_Key::F11,
|
||||||
|
Key::F12 => TFC_Key::F12,
|
||||||
|
Key::F2 => TFC_Key::F2,
|
||||||
|
Key::F3 => TFC_Key::F3,
|
||||||
|
Key::F4 => TFC_Key::F4,
|
||||||
|
Key::F5 => TFC_Key::F5,
|
||||||
|
Key::F6 => TFC_Key::F6,
|
||||||
|
Key::F7 => TFC_Key::F7,
|
||||||
|
Key::F8 => TFC_Key::F8,
|
||||||
|
Key::F9 => TFC_Key::F9,
|
||||||
|
Key::Home => TFC_Key::Home,
|
||||||
|
Key::LeftArrow => TFC_Key::LeftArrow,
|
||||||
|
Key::Option => TFC_Key::Alt,
|
||||||
|
Key::PageDown => TFC_Key::PageDown,
|
||||||
|
Key::PageUp => TFC_Key::PageUp,
|
||||||
|
Key::Return => TFC_Key::ReturnOrEnter,
|
||||||
|
Key::RightArrow => TFC_Key::RightArrow,
|
||||||
|
Key::Shift => TFC_Key::Shift,
|
||||||
|
Key::Space => TFC_Key::Space,
|
||||||
|
Key::Tab => TFC_Key::Tab,
|
||||||
|
Key::UpArrow => TFC_Key::UpArrow,
|
||||||
|
Key::Numpad0 => TFC_Key::N0,
|
||||||
|
Key::Numpad1 => TFC_Key::N1,
|
||||||
|
Key::Numpad2 => TFC_Key::N2,
|
||||||
|
Key::Numpad3 => TFC_Key::N3,
|
||||||
|
Key::Numpad4 => TFC_Key::N4,
|
||||||
|
Key::Numpad5 => TFC_Key::N5,
|
||||||
|
Key::Numpad6 => TFC_Key::N6,
|
||||||
|
Key::Numpad7 => TFC_Key::N7,
|
||||||
|
Key::Numpad8 => TFC_Key::N8,
|
||||||
|
Key::Numpad9 => TFC_Key::N9,
|
||||||
|
Key::Decimal => TFC_Key::NumpadDecimal,
|
||||||
|
// Key::Cancel => TFC_Key::Cancel,
|
||||||
|
Key::Clear => TFC_Key::NumpadClear,
|
||||||
|
Key::Pause => TFC_Key::PlayPause,
|
||||||
|
// Key::Kana => TFC_Key::,
|
||||||
|
// Key::Hangul => "Hangul",
|
||||||
|
// Key::Hanja => "Hanja",
|
||||||
|
// Key::Kanji => "Kanji",
|
||||||
|
// Key::Select => TFC_Key::Sel,
|
||||||
|
// Key::Print => TFC_Key::P,
|
||||||
|
// Key::Execute => "Execute",
|
||||||
|
// Key::Snapshot => "3270_PrintScreen",
|
||||||
|
// Key::Insert => TFC_Key:,
|
||||||
|
// Key::Help => "Help",
|
||||||
|
// Key::Separator => "KP_Separator",
|
||||||
|
// Key::Scroll => "Scroll_Lock",
|
||||||
|
// Key::NumLock => "Num_Lock",
|
||||||
|
Key::RWin => TFC_Key::Meta,
|
||||||
|
// Key::Apps => "Menu",
|
||||||
|
Key::Multiply => TFC_Key::NumpadMultiply,
|
||||||
|
Key::Add => TFC_Key::NumpadPlus,
|
||||||
|
Key::Subtract => TFC_Key::NumpadMinus,
|
||||||
|
Key::Divide => TFC_Key::NumpadDivide,
|
||||||
|
Key::Equals => TFC_Key::NumpadEquals,
|
||||||
|
Key::NumpadEnter => TFC_Key::NumpadEnter,
|
||||||
|
Key::RightShift => TFC_Key::RightShift,
|
||||||
|
Key::RightControl => TFC_Key::RightControl,
|
||||||
|
Key::RightAlt => TFC_Key::RightAlt,
|
||||||
|
Key::Command | Key::Super | Key::Windows | Key::Meta => TFC_Key::Meta,
|
||||||
|
_ => {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
log::info!("tfc_key_down_or_up: {:?}", key);
|
||||||
|
if down {
|
||||||
|
TFC_CONTEXT.lock().unwrap().key_down(key);
|
||||||
|
}
|
||||||
|
if up {
|
||||||
|
TFC_CONTEXT.lock().unwrap().key_up(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn legacy_keyboard_mode(evt: &KeyEvent) {
|
fn legacy_keyboard_mode(evt: &KeyEvent) {
|
||||||
let (click_capslock, click_numlock) = sync_status(evt);
|
let (click_capslock, click_numlock) = sync_status(evt);
|
||||||
|
|
||||||
@ -681,9 +782,15 @@ fn legacy_keyboard_mode(evt: &KeyEvent) {
|
|||||||
crate::platform::windows::try_change_desktop();
|
crate::platform::windows::try_change_desktop();
|
||||||
let mut en = ENIGO.lock().unwrap();
|
let mut en = ENIGO.lock().unwrap();
|
||||||
if click_capslock {
|
if click_capslock {
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
tfc_key_down_or_up(Key::CapsLock, true, true);
|
||||||
|
#[cfg(not(target_os = "linux"))]
|
||||||
en.key_click(Key::CapsLock);
|
en.key_click(Key::CapsLock);
|
||||||
}
|
}
|
||||||
if click_numlock {
|
if click_numlock {
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
tfc_key_down_or_up(Key::NumLock, true, true);
|
||||||
|
#[cfg(not(target_os = "linux"))]
|
||||||
en.key_click(Key::NumLock);
|
en.key_click(Key::NumLock);
|
||||||
}
|
}
|
||||||
// disable numlock if press home etc when numlock is on,
|
// disable numlock if press home etc when numlock is on,
|
||||||
@ -692,11 +799,11 @@ fn legacy_keyboard_mode(evt: &KeyEvent) {
|
|||||||
let mut disable_numlock = false;
|
let mut disable_numlock = false;
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
en.reset_flag();
|
en.reset_flag();
|
||||||
// When long-pressed the command key, then press and release
|
// When long-pressed the command key, then press and release
|
||||||
// the Tab key, there should be CGEventFlagCommand in the flag.
|
// the Tab key, there should be CGEventFlagCommand in the flag.
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
for ck in evt.modifiers.iter(){
|
for ck in evt.modifiers.iter() {
|
||||||
if let Some(key) = KEY_MAP.get(&ck.value()){
|
if let Some(key) = KEY_MAP.get(&ck.value()) {
|
||||||
en.add_flag(key);
|
en.add_flag(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -738,6 +845,9 @@ fn legacy_keyboard_mode(evt: &KeyEvent) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if !get_modifier_state(key.clone(), &mut en) {
|
if !get_modifier_state(key.clone(), &mut en) {
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
tfc_key_down_or_up(key.clone(), true, false);
|
||||||
|
#[cfg(not(target_os = "linux"))]
|
||||||
en.key_down(key.clone()).ok();
|
en.key_down(key.clone()).ok();
|
||||||
modifier_sleep();
|
modifier_sleep();
|
||||||
to_release.push(key);
|
to_release.push(key);
|
||||||
@ -749,7 +859,11 @@ fn legacy_keyboard_mode(evt: &KeyEvent) {
|
|||||||
}
|
}
|
||||||
#[cfg(not(target_os = "macos"))]
|
#[cfg(not(target_os = "macos"))]
|
||||||
if has_cap != en.get_key_state(Key::CapsLock) {
|
if has_cap != en.get_key_state(Key::CapsLock) {
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
tfc_key_down_or_up(Key::CapsLock, true, true);
|
||||||
|
#[cfg(not(target_os = "linux"))]
|
||||||
en.key_down(Key::CapsLock).ok();
|
en.key_down(Key::CapsLock).ok();
|
||||||
|
#[cfg(not(target_os = "linux"))]
|
||||||
en.key_up(Key::CapsLock);
|
en.key_up(Key::CapsLock);
|
||||||
}
|
}
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
@ -771,12 +885,18 @@ fn legacy_keyboard_mode(evt: &KeyEvent) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if evt.down {
|
if evt.down {
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
tfc_key_down_or_up(key.clone(), true, false);
|
||||||
|
#[cfg(not(target_os = "linux"))]
|
||||||
allow_err!(en.key_down(key.clone()));
|
allow_err!(en.key_down(key.clone()));
|
||||||
KEYS_DOWN
|
KEYS_DOWN
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.insert(ck.value() as _, Instant::now());
|
.insert(ck.value() as _, Instant::now());
|
||||||
} else {
|
} else {
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
tfc_key_down_or_up(key.clone(), false, true);
|
||||||
|
#[cfg(not(target_os = "linux"))]
|
||||||
en.key_up(key.clone());
|
en.key_up(key.clone());
|
||||||
KEYS_DOWN.lock().unwrap().remove(&(ck.value() as _));
|
KEYS_DOWN.lock().unwrap().remove(&(ck.value() as _));
|
||||||
}
|
}
|
||||||
@ -791,6 +911,14 @@ fn legacy_keyboard_mode(evt: &KeyEvent) {
|
|||||||
}
|
}
|
||||||
Some(key_event::Union::Chr(chr)) => {
|
Some(key_event::Union::Chr(chr)) => {
|
||||||
if evt.down {
|
if evt.down {
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
tfc_key_down_or_up(get_layout(chr), true, false);
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
KEYS_DOWN
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.insert(chr as u64 + KEY_CHAR_START, Instant::now());
|
||||||
|
#[cfg(not(target_os = "linux"))]
|
||||||
if en.key_down(get_layout(chr)).is_ok() {
|
if en.key_down(get_layout(chr)).is_ok() {
|
||||||
KEYS_DOWN
|
KEYS_DOWN
|
||||||
.lock()
|
.lock()
|
||||||
@ -808,6 +936,9 @@ fn legacy_keyboard_mode(evt: &KeyEvent) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
tfc_key_down_or_up(get_layout(chr), false, true);
|
||||||
|
#[cfg(not(target_os = "linux"))]
|
||||||
en.key_up(get_layout(chr));
|
en.key_up(get_layout(chr));
|
||||||
KEYS_DOWN
|
KEYS_DOWN
|
||||||
.lock()
|
.lock()
|
||||||
@ -827,6 +958,9 @@ fn legacy_keyboard_mode(evt: &KeyEvent) {
|
|||||||
}
|
}
|
||||||
#[cfg(not(target_os = "macos"))]
|
#[cfg(not(target_os = "macos"))]
|
||||||
for key in to_release {
|
for key in to_release {
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
tfc_key_down_or_up(key.clone(), false, true);
|
||||||
|
#[cfg(not(target_os = "linux"))]
|
||||||
en.key_up(key.clone());
|
en.key_up(key.clone());
|
||||||
}
|
}
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
@ -840,7 +974,11 @@ fn translate_keyboard_mode(evt: &KeyEvent) {
|
|||||||
let chr = char::from_u32(evt.chr()).unwrap_or_default();
|
let chr = char::from_u32(evt.chr()).unwrap_or_default();
|
||||||
// down(true)->press && press(false)-> release
|
// down(true)->press && press(false)-> release
|
||||||
if evt.down && !evt.press {
|
if evt.down && !evt.press {
|
||||||
KBD_CONTEXT.lock().unwrap().unicode_char(chr).expect("unicode_char_down error");
|
TFC_CONTEXT
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.unicode_char(chr)
|
||||||
|
.expect("unicode_char_down error");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user