translate mode, win2win, Send both unicode and virtual keycode to remote side

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2023-04-01 18:09:53 +08:00
parent d3ea3b1a80
commit ae53ec877b
4 changed files with 105 additions and 43 deletions

View File

@ -203,11 +203,13 @@ message KeyEvent {
bool press = 2;
oneof union {
ControlKey control_key = 3;
// high word, sym key code. win: virtual-key code, linux: keysym ?, macos:
// low word, position key code. win: scancode, linux: key code, macos: key code
// position key code. win: scancode, linux: key code, macos: key code
uint32 chr = 4;
uint32 unicode = 5;
string seq = 6;
// high word. virtual keycode
// low word. unicode
uint32 win2win_hotkey = 7;
}
repeated ControlKey modifiers = 8;
KeyboardMode mode = 9;

View File

@ -4,7 +4,7 @@ use crate::common::GrabState;
#[cfg(feature = "flutter")]
use crate::flutter::{CUR_SESSION_ID, SESSIONS};
#[cfg(target_os = "windows")]
use crate::platform::windows::get_char_by_vk;
use crate::platform::windows::{get_char_from_vk, get_unicode_from_vk};
#[cfg(not(any(feature = "flutter", feature = "cli")))]
use crate::ui::CUR_SESSION;
#[cfg(not(any(target_os = "android", target_os = "ios")))]
@ -874,7 +874,7 @@ fn try_fill_unicode(_peer: &str, event: &Event, key_event: &KeyEvent, events: &m
#[cfg(target_os = "windows")]
if _peer == OS_LOWER_LINUX {
if is_hot_key_modifiers_down() && unsafe { !IS_0X021D_DOWN } {
if let Some(chr) = get_char_by_vk(event.platform_code as u32) {
if let Some(chr) = get_char_from_vk(event.platform_code as u32) {
let mut evt = key_event.clone();
evt.set_seq(chr.to_string());
events.push(evt);
@ -885,6 +885,33 @@ fn try_fill_unicode(_peer: &str, event: &Event, key_event: &KeyEvent, events: &m
}
}
#[cfg(target_os = "windows")]
fn try_file_win2win_hotkey(
peer: &str,
event: &Event,
key_event: &KeyEvent,
events: &mut Vec<KeyEvent>,
) {
if peer == OS_LOWER_WINDOWS && is_hot_key_modifiers_down() && unsafe { !IS_0X021D_DOWN } {
let win2win_hotkey = match event.event_type {
EventType::KeyPress(..) => {
if let Some(unicode) = get_unicode_from_vk(event.platform_code as u32) {
Some((unicode as u32 & 0x0000FFFF) | (event.platform_code << 16))
} else {
None
}
}
EventType::KeyRelease(..) => Some(event.platform_code << 16),
_ => None,
};
if let Some(code) = win2win_hotkey {
let mut evt = key_event.clone();
evt.set_win2win_hotkey(code);
events.push(evt);
}
}
}
#[cfg(target_os = "windows")]
fn is_hot_key_modifiers_down() -> bool {
if rdev::get_modifier(Key::ControlLeft) || rdev::get_modifier(Key::ControlRight) {
@ -899,25 +926,6 @@ fn is_hot_key_modifiers_down() -> bool {
return false;
}
#[inline]
#[cfg(target_os = "windows")]
pub fn translate_key_code(peer: &str, event: &Event, key_event: KeyEvent) -> Option<KeyEvent> {
let mut key_event = map_keyboard_mode(peer, event, key_event)?;
let chr = if peer == OS_LOWER_WINDOWS {
(key_event.chr() & 0x0000FFFF) | ((event.platform_code as u32) << 16)
} else {
key_event.chr()
};
key_event.set_chr(chr);
Some(key_event)
}
#[inline]
#[cfg(not(target_os = "windows"))]
pub fn translate_key_code(peer: &str, event: &Event, key_event: KeyEvent) -> Option<KeyEvent> {
map_keyboard_mode(peer, event, key_event)
}
#[inline]
#[cfg(any(target_os = "linux", target_os = "windows"))]
fn is_altgr(event: &Event) -> bool {
@ -961,7 +969,7 @@ pub fn translate_keyboard_mode(peer: &str, event: &Event, key_event: KeyEvent) -
#[cfg(not(any(target_os = "android", target_os = "ios")))]
if is_numpad_key(&event) {
if let Some(evt) = translate_key_code(peer, event, key_event) {
if let Some(evt) = map_keyboard_mode(peer, event, key_event) {
events.push(evt);
}
return events;
@ -995,13 +1003,16 @@ pub fn translate_keyboard_mode(peer: &str, event: &Event, key_event: KeyEvent) -
}
}
#[cfg(target_os = "windows")]
try_file_win2win_hotkey(peer, event, &key_event, &mut events);
#[cfg(target_os = "macos")]
if !unsafe { IS_LEFT_OPTION_DOWN } {
try_fill_unicode(peer, event, &key_event, &mut events);
}
if events.is_empty() {
if let Some(evt) = translate_key_code(peer, event, key_event) {
if let Some(evt) = map_keyboard_mode(peer, event, key_event) {
events.push(evt);
}
}

View File

@ -1229,7 +1229,7 @@ sc delete {app_name}
}
pub fn run_after_install() -> ResultType<()> {
let (_, _, _, exe,_) = get_install_info();
let (_, _, _, exe, _) = get_install_info();
run_cmds(get_after_install(&exe), true, "after_install")
}
@ -2130,7 +2130,25 @@ mod cert {
}
}
pub fn get_char_by_vk(vk: u32) -> Option<char> {
#[inline]
pub fn get_char_from_vk(vk: u32) -> Option<char> {
get_char_from_unicode(get_unicode_from_vk(vk)?)
}
pub fn get_char_from_unicode(unicode: u16) -> Option<char> {
let buff = [unicode];
if let Some(chr) = String::from_utf16(&buff[..1]).ok()?.chars().next() {
if chr.is_control() {
return None;
} else {
Some(chr)
}
} else {
None
}
}
pub fn get_unicode_from_vk(vk: u32) -> Option<u16> {
const BUF_LEN: i32 = 32;
let mut buff = [0_u16; BUF_LEN as usize];
let buff_ptr = buff.as_mut_ptr();
@ -2155,19 +2173,7 @@ pub fn get_char_by_vk(vk: u32) -> Option<char> {
ToUnicodeEx(vk, 0x00, &state as _, buff_ptr, BUF_LEN, 0, layout)
};
if len == 1 {
if let Some(chr) = String::from_utf16(&buff[..len as usize])
.ok()?
.chars()
.next()
{
if chr.is_control() {
return None;
} else {
Some(chr)
}
} else {
None
}
Some(buff[0])
} else {
None
}
@ -2190,10 +2196,10 @@ mod tests {
}
#[test]
fn test_get_char_by_vk() {
let chr = get_char_by_vk(0x41); // VK_A
fn test_get_unicode_char_by_vk() {
let chr = get_char_from_vk(0x41); // VK_A
assert_eq!(chr, Some('a'));
let chr = get_char_by_vk(VK_ESCAPE as u32); // VK_ESC
let chr = get_char_from_vk(VK_ESCAPE as u32); // VK_ESC
assert_eq!(chr, None)
}
}

View File

@ -1,6 +1,8 @@
use super::*;
#[cfg(target_os = "linux")]
use crate::common::IS_X11;
#[cfg(target_os = "windows")]
use crate::platform::windows::get_char_from_unicode;
#[cfg(target_os = "macos")]
use dispatch::Queue;
use enigo::{Enigo, Key, KeyboardControllable, MouseButton, MouseControllable};
@ -1280,12 +1282,53 @@ fn translate_keyboard_mode(evt: &KeyEvent) {
Some(key_event::Union::Unicode(..)) => {
// Do not handle unicode for now.
}
#[cfg(target_os = "windows")]
Some(key_event::Union::Win2winHotkey(code)) => {
simulate_win2win_hotkey(*code, evt.down);
}
_ => {
log::debug!("Unreachable. Unexpected key event {:?}", &evt);
}
}
}
#[cfg(target_os = "windows")]
fn simulate_win2win_hotkey(code: u32, down: bool) {
let mut simulated = false;
let unicode: u16 = (code & 0x0000FFFF) as u16;
if unicode != 0 {
// Try convert unicode to virtual keycode first.
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-vkkeyscanw
let res = unsafe { winapi::um::winuser::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])));
}
}
simulated = true;
}
}
if simulated {
let keycode: u16 = ((code >> 16) & 0x0000FFFF) as u16;
allow_err!(rdev::simulate_code(Some(keycode), None, down));
}
}
pub fn handle_key_(evt: &KeyEvent) {
if EXITING.load(Ordering::SeqCst) {
return;