From 92c4ee15608d2b6eea0e7c6b1159ddee2f124989 Mon Sep 17 00:00:00 2001 From: Asura Date: Mon, 1 Aug 2022 00:52:07 +0800 Subject: [PATCH] Fix character generation, altgr only takes effect locally --- Cargo.lock | 2 +- src/ui/remote.rs | 41 ++++++++++++++++++++++++++++++++--------- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 833cbe12a..9d807f12e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3795,7 +3795,7 @@ dependencies = [ [[package]] name = "rdev" version = "0.5.0-2" -source = "git+https://github.com/asur4s/rdev#c3a896bcb4a10d171dee1aa685c90abadf8946d2" +source = "git+https://github.com/asur4s/rdev#d009906ba983f26c7b6f6f1a5e3c76bf43164294" dependencies = [ "cocoa", "core-foundation 0.9.3", diff --git a/src/ui/remote.rs b/src/ui/remote.rs index 1b5331609..213158bfc 100644 --- a/src/ui/remote.rs +++ b/src/ui/remote.rs @@ -43,7 +43,7 @@ use hbb_common::{ Stream, }; use hbb_common::{config::TransferSerde, fs::TransferJobMeta}; -use rdev::{Event, EventType::*, Key as RdevKey}; +use rdev::{Event, EventType::*, Key as RdevKey, Keyboard as RdevKeyboard, KeyboardState}; #[cfg(windows)] use crate::clipboard_file::*; @@ -58,6 +58,7 @@ lazy_static::lazy_static! { static ref ENIGO: Arc> = Arc::new(Mutex::new(Enigo::new())); static ref VIDEO: Arc>> = Default::default(); static ref TO_RELEASE: Arc>> = Arc::new(Mutex::new(HashSet::::new())); + static ref KEYBOARD: Arc> = Arc::new(Mutex::new(RdevKeyboard::new().unwrap())); } fn get_key_state(key: enigo::Key) -> bool { @@ -333,9 +334,6 @@ impl Handler { _ => return, }; - #[cfg(target_os = "windows")] - let _key = rdev::get_win_key(evt.code.into(), evt.scan_code); - me.key_down_or_up(down, _key, evt); }; if let Err(error) = rdev::listen(func) { @@ -785,7 +783,7 @@ impl Handler { fn leave(&mut self) { for key in TO_RELEASE.lock().unwrap().iter() { - self.map_keyboard_mode(false, *key) + self.map_keyboard_mode(false, *key, None) } #[cfg(windows)] crate::platform::windows::stop_system_key_propagate(false); @@ -1036,8 +1034,15 @@ impl Handler { } } - fn map_keyboard_mode(&mut self, down_or_up: bool, key: RdevKey) { + fn map_keyboard_mode(&mut self, down_or_up: bool, key: RdevKey, evt: Option) { // map mode(1): Send keycode according to the peer platform. + #[cfg(target_os = "windows")] + let key = if let Some(e) = evt { + rdev::get_win_key(e.code.into(), e.scan_code) + } else { + key + }; + let peer = self.peer_platform(); let mut key_event = KeyEvent::new(); @@ -1067,8 +1072,20 @@ impl Handler { fn translate_keyboard_mode(&mut self, down_or_up: bool, key: RdevKey, evt: Event) { // translate mode(2): locally generated characters are send to the peer. - let string = evt.name.unwrap_or_default(); + // get char + let string = match KEYBOARD.lock() { + Ok(mut keyboard) => { + let string = keyboard.add(&evt.event_type).unwrap_or_default(); + if keyboard.last_is_dead && string == "" { + return; + } + string + } + Err(_) => "".to_owned(), + }; + + // maybe two string let chars = if string == "" { None } else { @@ -1096,7 +1113,13 @@ impl Handler { } else { TO_RELEASE.lock().unwrap().remove(&key); } - self.map_keyboard_mode(down_or_up, key); + // algr without action + // Control left + if key == RdevKey::AltGr || evt.scan_code == 541 { + return; + } + dbg!(key); + self.map_keyboard_mode(down_or_up, key, None); } } @@ -1369,7 +1392,7 @@ impl Handler { } else { TO_RELEASE.lock().unwrap().remove(&key); } - self.map_keyboard_mode(down_or_up, key); + self.map_keyboard_mode(down_or_up, key, Some(evt)); } KeyboardMode::Legacy => self.legacy_keyboard_mode(down_or_up, key, evt), KeyboardMode::Translate => {