Merge pull request #4119 from fufesou/feat/translate_mode_scancode

Feat/translate mode scancode
This commit is contained in:
RustDesk 2023-04-20 13:57:02 +08:00 committed by GitHub
commit bf58b1086b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 75 deletions

2
Cargo.lock generated
View File

@ -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#2b9d473dfd9a49649627f0684f49e920494837e2"
dependencies = [
"cocoa",
"core-foundation 0.9.3",

View File

@ -9,8 +9,10 @@ use crate::{
ui_interface::{self, *},
};
use flutter_rust_bridge::{StreamSink, SyncReturn};
#[cfg(feature = "plugin_framework")]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
use hbb_common::allow_err;
use hbb_common::{
allow_err,
config::{self, LocalConfig, PeerConfig, PeerInfoSerde, ONLINE},
fs, log,
message_proto::KeyboardMode,
@ -1395,11 +1397,11 @@ 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<u8>) {
pub fn plugin_event(_id: String, _event: Vec<u8>) {
#[cfg(feature = "plugin_framework")]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
allow_err!(crate::plugin::handle_ui_event(&id, &event));
allow_err!(crate::plugin::handle_ui_event(&_id, &_event));
}
}

View File

@ -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<Mutex<u32>> = 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;
}
}