diff --git a/src/keyboard.rs b/src/keyboard.rs index 93bcf7ff0..cdedc4fdb 100644 --- a/src/keyboard.rs +++ b/src/keyboard.rs @@ -365,6 +365,21 @@ pub fn get_keyboard_mode_enum() -> KeyboardMode { } } +#[inline] +pub fn is_modifier(key: &rdev::Key) -> bool { + matches!( + key, + Key::ShiftLeft + | Key::ShiftRight + | Key::ControlLeft + | Key::ControlRight + | Key::MetaLeft + | Key::MetaRight + | Key::Alt + | Key::AltGr + ) +} + #[inline] #[cfg(not(any(target_os = "android", target_os = "ios")))] fn is_numpad_key(event: &Event) -> bool { @@ -991,3 +1006,13 @@ pub fn translate_keyboard_mode(peer: &str, event: &Event, key_event: KeyEvent) - } events } + +#[cfg(not(any(target_os = "android", target_os = "ios")))] +pub fn keycode_to_rdev_key(keycode: u32) -> Key { + #[cfg(target_os = "windows")] + return rdev::win_key_from_scancode(keycode); + #[cfg(target_os = "linux")] + return rdev::linux_key_from_code(keycode); + #[cfg(target_os = "macos")] + return rdev::macos_key_from_code(keycode.try_into().unwrap_or_default()); +} diff --git a/src/lang.rs b/src/lang.rs index 972510325..4776eb89e 100644 --- a/src/lang.rs +++ b/src/lang.rs @@ -5,12 +5,12 @@ mod cn; mod cs; mod da; mod de; +mod el; mod en; mod eo; mod es; mod fa; mod fr; -mod el; mod hu; mod id; mod it; diff --git a/src/platform/mod.rs b/src/platform/mod.rs index f8fe7a6dd..777de3b01 100644 --- a/src/platform/mod.rs +++ b/src/platform/mod.rs @@ -37,7 +37,7 @@ pub fn breakdown_callback() { #[cfg(target_os = "linux")] crate::input_service::clear_remapped_keycode(); #[cfg(not(any(target_os = "android", target_os = "ios")))] - crate::input_service::release_modifiers(); + crate::input_service::release_device_modifiers(); } // Android diff --git a/src/server/connection.rs b/src/server/connection.rs index 539d96ddd..d1ae4d6a3 100644 --- a/src/server/connection.rs +++ b/src/server/connection.rs @@ -39,6 +39,7 @@ use sha2::{Digest, Sha256}; #[cfg(not(any(target_os = "android", target_os = "ios")))] use std::sync::atomic::Ordering; use std::{ + collections::HashSet, num::NonZeroI64, sync::{atomic::AtomicI64, mpsc as std_mpsc}, }; @@ -132,6 +133,7 @@ pub struct Connection { voice_call_request_timestamp: Option, audio_input_device_before_voice_call: Option, options_in_login: Option, + pressed_modifiers: HashSet, } impl ConnInner { @@ -243,6 +245,7 @@ impl Connection { voice_call_request_timestamp: None, audio_input_device_before_voice_call: None, options_in_login: None, + pressed_modifiers: Default::default(), }; #[cfg(not(any(target_os = "android", target_os = "ios")))] tokio::spawn(async move { @@ -618,7 +621,6 @@ impl Connection { } #[cfg(target_os = "linux")] clear_remapped_keycode(); - release_modifiers(); log::info!("Input thread exited"); } @@ -1357,6 +1359,30 @@ impl Connection { } else { me.press }; + + let key = match me.mode.enum_value_or_default() { + KeyboardMode::Map => { + Some(crate::keyboard::keycode_to_rdev_key(me.chr())) + } + KeyboardMode::Translate => { + if let Some(key_event::Union::Chr(code)) = me.union { + Some(crate::keyboard::keycode_to_rdev_key(code & 0x0000FFFF)) + } else { + None + } + } + _ => None, + } + .filter(crate::keyboard::is_modifier); + + if let Some(key) = key { + if is_press { + self.pressed_modifiers.insert(key); + } else { + self.pressed_modifiers.remove(&key); + } + } + if is_press { match me.union { Some(key_event::Union::Unicode(_)) @@ -2011,6 +2037,14 @@ impl Connection { }) .count(); } + + #[cfg(not(any(target_os = "android", target_os = "ios")))] + fn release_pressed_modifiers(&mut self) { + for modifier in self.pressed_modifiers.iter() { + rdev::simulate(&rdev::EventType::KeyRelease(*modifier)).ok(); + } + self.pressed_modifiers.clear(); + } } pub fn insert_switch_sides_uuid(id: String, uuid: uuid::Uuid) { @@ -2208,3 +2242,10 @@ impl Default for PortableState { } } } + +impl Drop for Connection { + fn drop(&mut self) { + #[cfg(not(any(target_os = "android", target_os = "ios")))] + self.release_pressed_modifiers(); + } +} diff --git a/src/server/input_service.rs b/src/server/input_service.rs index 25b41d0ef..5bc486516 100644 --- a/src/server/input_service.rs +++ b/src/server/input_service.rs @@ -551,7 +551,7 @@ fn record_key_to_key(record_key: u64) -> Option { } } -pub fn release_modifiers() { +pub fn release_device_modifiers() { let mut en = ENIGO.lock().unwrap(); for modifier in [ Key::Shift,