Skip to content

Commit

Permalink
macOS: Immediatly try with TISCopyCurrentKeyboardLayoutInputSource
Browse files Browse the repository at this point in the history
  • Loading branch information
pentamassiv committed Mar 7, 2023
1 parent c4b5c0f commit 0e38660
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions src/macos/macos_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ pub const kCFStringEncodingUTF8: u32 = 134_217_984;
#[allow(improper_ctypes)]
#[link(name = "Carbon", kind = "framework")]
extern "C" {
fn TISCopyCurrentKeyboardInputSource() -> TISInputSourceRef;
fn TISCopyCurrentKeyboardLayoutInputSource() -> TISInputSourceRef;

// extern void *
Expand Down Expand Up @@ -514,20 +513,16 @@ impl Enigo {

#[allow(clippy::unused_self)]
fn create_string_for_key(&self, keycode: u16, modifier: u32) -> CFStringRef {
let mut current_keyboard = unsafe { TISCopyCurrentKeyboardInputSource() };
let mut layout_data = unsafe {
// TISGetInputSourceProperty returns null when an input method is used as well.
// Using TISCopyCurrentKeyboardLayoutInputSource to fix NULL return.
// See also: https://github.com/microsoft/node-native-keymap/blob/089d802efd387df4dce1f0e31898c66e28b3f67f/src/keyboard_mac.mm#L90

let current_keyboard = unsafe { TISCopyCurrentKeyboardLayoutInputSource() };
let layout_data = unsafe {
TISGetInputSourceProperty(current_keyboard, kTISPropertyUnicodeKeyLayoutData)
};
if layout_data.is_null() {
// TISGetInputSourceProperty returns null with some keyboard layout.
// Using TISCopyCurrentKeyboardLayoutInputSource to fix NULL return.
// See also: https://github.com/microsoft/node-native-keymap/blob/089d802efd387df4dce1f0e31898c66e28b3f67f/src/keyboard_mac.mm#L90
current_keyboard = unsafe { TISCopyCurrentKeyboardLayoutInputSource() };
layout_data = unsafe {
TISGetInputSourceProperty(current_keyboard, kTISPropertyUnicodeKeyLayoutData)
};
debug_assert!(!layout_data.is_null());
}
assert!(!layout_data.is_null());

let keyboard_layout = unsafe { CFDataGetBytePtr(layout_data) };

let mut keys_down: UInt32 = 0;
Expand Down

0 comments on commit 0e38660

Please sign in to comment.