righ key mapping and fix mac crash

This commit is contained in:
rustdesk
2021-05-26 12:42:21 +08:00
parent d825d30e53
commit 8708fb366a
10 changed files with 128 additions and 11 deletions

View File

@@ -347,7 +347,7 @@ pub enum Key {
///
Clear,
///
Menu,
Menu, // deprecated, use alt instead
///
Pause,
///
@@ -409,6 +409,12 @@ pub enum Key {
///
NumpadEnter,
///
RightShift,
///
RightControl,
///
RightAlt,
///
/// Function, /// mac
/// keyboard layout dependent key
Layout(char),
@@ -485,7 +491,7 @@ impl Enigo {
/// ```
pub fn new() -> Self {
#[cfg(any(target_os = "android", target_os = "ios"))]
return Enigo{};
return Enigo {};
#[cfg(not(any(target_os = "android", target_os = "ios")))]
Self::default()
}

View File

@@ -260,7 +260,7 @@ fn keysequence<'a>(key: Key) -> Cow<'a, str> {
Key::Mute => "",
Key::Scroll => "Scroll_Lock",
Key::NumLock => "Num_Lock",
Key::RWin => "",
Key::RWin => "Super_R",
Key::Apps => "",
Key::Multiply => "KP_Multiply",
Key::Add => "KP_Add",
@@ -268,6 +268,9 @@ fn keysequence<'a>(key: Key) -> Cow<'a, str> {
Key::Divide => "KP_Divide",
Key::Equals => "KP_Equal",
Key::NumpadEnter => "KP_Enter",
Key::RightShift => "Shift_R",
Key::RightControl => "Control_R",
Key::RightAlt => "Alt_R",
Key::Command | Key::Super | Key::Windows | Key::Meta => "Super",

View File

@@ -70,3 +70,4 @@ pub const kVK_ANSI_KeypadDivide: u16 = 0x4B;
pub const kVK_ANSI_KeypadEnter: u16 = 0x4C;
pub const kVK_ANSI_KeypadMinus: u16 = 0x4E;
pub const kVK_ANSI_KeypadEquals: u16 = 0x51;
pub const kVK_RIGHT_COMMAND: u16 = 0x36;

View File

@@ -142,6 +142,8 @@ pub const kCFStringEncodingUTF8: u32 = 134_217_984;
#[link(name = "Carbon", kind = "framework")]
extern "C" {
fn TISCopyCurrentKeyboardInputSource() -> TISInputSourceRef;
fn TISCopyCurrentKeyboardLayoutInputSource() -> TISInputSourceRef;
fn TISCopyCurrentASCIICapableKeyboardLayoutInputSource() -> TISInputSourceRef;
// extern void *
// TISGetInputSourceProperty(
@@ -583,6 +585,10 @@ impl Enigo {
Key::Subtract => kVK_ANSI_KeypadMinus,
Key::Equals => kVK_ANSI_KeypadEquals,
Key::NumLock => kVK_ANSI_KeypadClear,
Key::RWin => kVK_RIGHT_COMMAND,
Key::RightShift => kVK_RightShift,
Key::RightControl => kVK_RightControl,
Key::RightAlt => kVK_RightOption,
Key::Raw(raw_keycode) => raw_keycode,
Key::Layout(c) => self.get_layoutdependent_keycode(c.to_string()),
@@ -600,6 +606,7 @@ impl Enigo {
}
fn init_map(&mut self) {
println!("init_map");
self.keycode_to_string_map.insert("".to_owned(), 0);
// loop through every keycode (0 - 127)
for keycode in 0..128 {
@@ -649,14 +656,44 @@ impl Enigo {
fn create_string_for_key(&self, keycode: u16, modifier: u32) -> CFStringRef {
let current_keyboard = unsafe { TISCopyCurrentKeyboardInputSource() };
let layout_data = unsafe {
TISGetInputSourceProperty(current_keyboard, kTISPropertyUnicodeKeyLayoutData)
};
let mut layout_data = std::ptr::null_mut();
if !current_keyboard.is_null() {
layout_data = unsafe {
TISGetInputSourceProperty(current_keyboard, kTISPropertyUnicodeKeyLayoutData)
};
}
if layout_data.is_null() {
// https://github.com/microsoft/vscode/issues/23833
let current_keyboard = unsafe { TISCopyCurrentKeyboardLayoutInputSource() };
if !current_keyboard.is_null() {
layout_data = unsafe {
TISGetInputSourceProperty(current_keyboard, kTISPropertyUnicodeKeyLayoutData)
};
}
}
if layout_data.is_null() {
let current_keyboard = unsafe { TISCopyCurrentASCIICapableKeyboardLayoutInputSource() };
if !current_keyboard.is_null() {
layout_data = unsafe {
TISGetInputSourceProperty(current_keyboard, kTISPropertyUnicodeKeyLayoutData)
};
}
}
if layout_data.is_null() {
// to-do: try out manual mapping in https://github.com/stweil/OSXvnc
// we do see crash like this, also not easy to reproduce, no sure if it related
/*
0 rustdesk 0x000000010f921bc9 std::collections::hash::map::HashMap$LT$K$C$V$C$S$GT$::insert::h84e28c51a3292e7a + 473
1 rustdesk 0x000000010f921884 enigo::macos::macos_impl::Enigo::key_to_keycode::h85ead82e9b1075ae + 1428
2 rustdesk 0x000000010f922a8c _$LT$enigo..macos..macos_impl..Enigo$u20$as$u20$enigo..KeyboardControllable$GT$::key_down::h54f24da6d274b948 + 44
*/
return std::ptr::null() as _;
}
let keyboard_layout = unsafe { CFDataGetBytePtr(layout_data) };
let mut keys_down: UInt32 = 0;
// let mut chars: *mut c_void;//[UniChar; 4];
let mut chars: u16 = 0;
// let mut chars: *mut c_void;//[UniChar; 4];
let mut real_length: UniCharCount = 0;
unsafe {
UCKeyTranslate(

View File

@@ -7,9 +7,14 @@ pub const EVK_BACK: u16 = 0x08;
pub const EVK_ESCAPE: u16 = 0x1b;
pub const EVK_LWIN: u16 = 0x5b;
pub const EVK_SHIFT: u16 = 0x10;
//pub const EVK_LSHIFT: u16 = 0xa0;
pub const EVK_RSHIFT: u16 = 0xa1;
//pub const EVK_LMENU: u16 = 0xa4;
pub const EVK_RMENU: u16 = 0xa5;
pub const EVK_CAPITAL: u16 = 0x14;
pub const EVK_MENU: u16 = 0x12;
pub const EVK_LCONTROL: u16 = 0xa2;
pub const EVK_RCONTROL: u16 = 0xa3;
pub const EVK_HOME: u16 = 0x24;
pub const EVK_PRIOR: u16 = 0x21;
pub const EVK_NEXT: u16 = 0x22;

View File

@@ -338,10 +338,14 @@ impl Enigo {
Key::Divide => EVK_DIVIDE,
Key::NumpadEnter => EVK_RETURN,
Key::Equals => '=' as _,
Key::RightShift => EVK_RSHIFT,
Key::RightControl => EVK_RCONTROL,
Key::RightAlt => EVK_RMENU,
Key::Raw(raw_keycode) => raw_keycode,
Key::Layout(c) => self.get_layoutdependent_keycode(c.to_string()),
Key::Super | Key::Command | Key::Windows | Key::Meta => EVK_LWIN,
_ => 0,
}
}