From d8dbf72ad2f0099e11bb765871eed6c9934f6f64 Mon Sep 17 00:00:00 2001 From: fufesou Date: Thu, 20 Apr 2023 12:15:38 +0800 Subject: [PATCH] simulate scancode for translate mode Signed-off-by: fufesou --- Cargo.lock | 2 +- src/flutter_ffi.rs | 5 ++- src/server/input_service.rs | 72 ++----------------------------------- 3 files changed, 5 insertions(+), 74 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 299d99c14..f1fb3595e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4840,7 +4840,7 @@ dependencies = [ [[package]] name = "rdev" version = "0.5.0-2" -source = "git+https://github.com/fufesou/rdev#aeea78dec3e731dd616da09668bbbd835a773d6c" +source = "git+https://github.com/fufesou/rdev#b866494215329f3e8e4863c3c5fb98a10297610c" dependencies = [ "cocoa", "core-foundation 0.9.3", diff --git a/src/flutter_ffi.rs b/src/flutter_ffi.rs index 2b522d470..aaee8eaff 100644 --- a/src/flutter_ffi.rs +++ b/src/flutter_ffi.rs @@ -10,7 +10,6 @@ use crate::{ }; use flutter_rust_bridge::{StreamSink, SyncReturn}; use hbb_common::{ - allow_err, config::{self, LocalConfig, PeerConfig, PeerInfoSerde, ONLINE}, fs, log, message_proto::KeyboardMode, @@ -1396,10 +1395,10 @@ pub fn send_url_scheme(_url: String) { #[inline] #[cfg(not(any(target_os = "android", target_os = "ios")))] -pub fn plugin_event(id: String, event: Vec) { +pub fn plugin_event(_id: String, _event: Vec) { #[cfg(feature = "plugin_framework")] { - allow_err!(crate::plugin::handle_ui_event(&id, &event)); + allow_err!(crate::plugin::handle_ui_event(&_id, &_event)); } } diff --git a/src/server/input_service.rs b/src/server/input_service.rs index ab5f7e287..8096237f6 100644 --- a/src/server/input_service.rs +++ b/src/server/input_service.rs @@ -15,19 +15,9 @@ use std::{ thread, time::{self, Duration, Instant}, }; -#[cfg(target_os = "windows")] -use winapi::um::winuser::{ - ActivateKeyboardLayout, GetForegroundWindow, GetKeyboardLayout, GetWindowThreadProcessId, - VkKeyScanW, -}; const INVALID_CURSOR_POS: i32 = i32::MIN; -#[cfg(target_os = "windows")] -lazy_static::lazy_static! { - static ref LAST_HKL: Arc> = Arc::new(Mutex::new(0)); -} - #[derive(Default)] struct StateCursor { hcursor: u64, @@ -1244,45 +1234,7 @@ fn translate_process_code(code: u32, down: bool) { }; } -#[cfg(target_os = "windows")] -fn check_update_input_layout() { - unsafe { - let foreground_thread_id = - GetWindowThreadProcessId(GetForegroundWindow(), std::ptr::null_mut()); - let layout = GetKeyboardLayout(foreground_thread_id); - let layout_u32 = layout as u32; - let mut last_layout_lock = LAST_HKL.lock().unwrap(); - if *last_layout_lock == 0 || *last_layout_lock != layout_u32 { - let res = ActivateKeyboardLayout(layout, 0); - if res == layout { - *last_layout_lock = layout_u32; - } else { - log::error!("Failed to call ActivateKeyboardLayout, {}", layout_u32); - } - } - } -} - fn translate_keyboard_mode(evt: &KeyEvent) { - // --server could not detect the input layout change. - // This is a temporary workaround. - // - // There may be a better way to detect and handle the input layout change. - // while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0) - // { - // ... - // if (msg.message == WM_INPUTLANGCHANGE) - // { - // // handle WM_INPUTLANGCHANGE message here - // check_update_input_layout(); - // } - // TranslateMessage(&msg); - // DispatchMessage(&msg); - // ... - // } - #[cfg(target_os = "windows")] - check_update_input_layout(); - match &evt.union { Some(key_event::Union::Seq(seq)) => { // Fr -> US @@ -1305,7 +1257,7 @@ fn translate_keyboard_mode(evt: &KeyEvent) { } for chr in seq.chars() { #[cfg(target_os = "windows")] - rdev::simulate_char(chr).ok(); + rdev::simulate_char(chr, true).ok(); #[cfg(target_os = "linux")] en.key_click(Key::Layout(chr)); } @@ -1334,27 +1286,7 @@ fn translate_keyboard_mode(evt: &KeyEvent) { fn simulate_win2win_hotkey(code: u32, down: bool) { let unicode: u16 = (code & 0x0000FFFF) as u16; if down { - // Try convert unicode to virtual keycode first. - // https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-vkkeyscanw - let res = unsafe { VkKeyScanW(unicode) }; - if res as u16 != 0xFFFF { - let vk = res & 0x00FF; - let flag = res >> 8; - let modifiers = [rdev::Key::ShiftLeft, rdev::Key::ControlLeft, rdev::Key::Alt]; - let mod_len = modifiers.len(); - for pos in 0..mod_len { - if flag & (0x0001 << pos) != 0 { - allow_err!(rdev::simulate(&EventType::KeyPress(modifiers[pos]))); - } - } - allow_err!(rdev::simulate_code(Some(vk as _), None, true)); - allow_err!(rdev::simulate_code(Some(vk as _), None, false)); - for pos in 0..mod_len { - let rpos = mod_len - 1 - pos; - if flag & (0x0001 << rpos) != 0 { - allow_err!(rdev::simulate(&EventType::KeyRelease(modifiers[rpos]))); - } - } + if rdev::simulate_key_unicode(unicode, false).is_ok() { return; } }