forked from enigo-rs/enigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacos_impl.rs
677 lines (595 loc) · 24.3 KB
/
macos_impl.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
use std::os::raw::{c_char, c_int, c_uchar, c_uint, c_ulong, c_ushort, c_void};
use std::{
thread,
time::{Duration, Instant},
};
use core_graphics::display::{CFIndex, CGDisplay, CGPoint};
use core_graphics::event::{
CGEvent, CGEventTapLocation, CGEventType, CGKeyCode, CGMouseButton, EventField, KeyCode,
ScrollEventUnit,
};
use core_graphics::event_source::{CGEventSource, CGEventSourceStateID};
use icrate::AppKit;
use log::{debug, error, info};
use crate::{
Axis, Button, Coordinate, Direction, InputError, InputResult, Key, Keyboard, Mouse,
NewConError, Settings,
};
type CFDataRef = *const c_void;
#[repr(C)]
struct __TISInputSource;
type TISInputSourceRef = *const __TISInputSource;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
struct __CFString([u8; 0]);
type CFStringRef = *const __CFString;
type Boolean = c_uchar;
type UInt8 = c_uchar;
type SInt32 = c_int;
type UInt16 = c_ushort;
type UInt32 = c_uint;
type UniChar = UInt16;
type UniCharCount = c_ulong;
type OptionBits = UInt32;
type OSStatus = SInt32;
type CFStringEncoding = UInt32;
const TRUE: c_uint = 1;
#[allow(non_snake_case)]
#[repr(C)]
#[derive(Debug, Clone, Copy)]
struct UCKeyboardTypeHeader {
keyboardTypeFirst: UInt32,
keyboardTypeLast: UInt32,
keyModifiersToTableNumOffset: UInt32,
keyToCharTableIndexOffset: UInt32,
keyStateRecordsIndexOffset: UInt32,
keyStateTerminatorsOffset: UInt32,
keySequenceDataIndexOffset: UInt32,
}
#[allow(non_snake_case)]
#[repr(C)]
#[derive(Debug, Clone, Copy)]
struct UCKeyboardLayout {
keyLayoutHeaderFormat: UInt16,
keyLayoutDataVersion: UInt16,
keyLayoutFeatureInfoOffset: UInt32,
keyboardTypeCount: UInt32,
keyboardTypeList: [UCKeyboardTypeHeader; 1usize],
}
#[allow(non_upper_case_globals)]
const kUCKeyTranslateNoDeadKeysBit: u32 = 0;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
struct __CFAllocator([u8; 0]);
type CFAllocatorRef = *const __CFAllocator;
#[allow(non_upper_case_globals)]
const kCFStringEncodingUTF8: u32 = 0x0800_0100;
#[allow(improper_ctypes)]
#[link(name = "Carbon", kind = "framework")]
extern "C" {
fn TISCopyCurrentKeyboardInputSource() -> TISInputSourceRef;
fn TISCopyCurrentKeyboardLayoutInputSource() -> TISInputSourceRef;
#[allow(non_upper_case_globals)]
static kTISPropertyUnicodeKeyLayoutData: CFStringRef;
#[allow(non_snake_case)]
fn TISGetInputSourceProperty(
inputSource: TISInputSourceRef,
propertyKey: CFStringRef,
) -> *mut c_void;
#[allow(non_snake_case)]
fn CFDataGetBytePtr(theData: CFDataRef) -> *const UInt8;
#[allow(non_snake_case)]
fn UCKeyTranslate(
keyLayoutPtr: *const UInt8, //*const UCKeyboardLayout,
virtualKeyCode: UInt16,
keyAction: UInt16,
modifierKeyState: UInt32,
keyboardType: UInt32,
keyTranslateOptions: OptionBits,
deadKeyState: *mut UInt32,
maxStringLength: UniCharCount,
actualStringLength: *mut UniCharCount,
unicodeString: *mut UniChar,
) -> OSStatus;
fn LMGetKbdType() -> UInt8;
#[allow(non_snake_case)]
fn CFStringCreateWithCharacters(
alloc: CFAllocatorRef,
chars: *const UniChar,
numChars: CFIndex,
) -> CFStringRef;
#[allow(non_upper_case_globals)]
static kCFAllocatorDefault: CFAllocatorRef;
#[allow(non_snake_case)]
fn CFStringGetLength(theString: CFStringRef) -> CFIndex;
#[allow(non_snake_case)]
fn CFStringGetCString(
theString: CFStringRef,
buffer: *mut c_char,
bufferSize: CFIndex,
encoding: CFStringEncoding,
) -> Boolean;
}
/// The main struct for handling the event emitting
pub struct Enigo {
delay: u64,
event_source: CGEventSource,
display: CGDisplay,
held: (Vec<Key>, Vec<CGKeyCode>), // Currently held keys
release_keys_when_dropped: bool,
double_click_delay: Duration,
// TODO: Use mem::variant_count::<Button>() here instead of 7 once it is stabalized
last_mouse_click: [(i64, Instant); 7], /* For each of the seven Button variants, we
* store the last time the button was clicked and
* the nth click that was
* This information is needed to
* determine double clicks and handle cases where
* another button is clicked while the other one has
* not yet been released */
}
impl Mouse for Enigo {
// Sends a button event to the X11 server via `XTest` extension
fn button(&mut self, button: Button, direction: Direction) -> InputResult<()> {
debug!("\x1b[93mbutton(button: {button:?}, direction: {direction:?})\x1b[0m");
let (current_x, current_y) = self.location()?;
if direction == Direction::Click || direction == Direction::Press {
let click_count = self.nth_button_press(button, Direction::Press);
let (button, event_type) = match button {
Button::Left => (CGMouseButton::Left, CGEventType::LeftMouseDown),
Button::Middle => (CGMouseButton::Center, CGEventType::OtherMouseDown),
Button::Right => (CGMouseButton::Right, CGEventType::RightMouseDown),
Button::ScrollUp => return self.scroll(-1, Axis::Vertical),
Button::ScrollDown => return self.scroll(1, Axis::Vertical),
Button::ScrollLeft => return self.scroll(-1, Axis::Horizontal),
Button::ScrollRight => return self.scroll(1, Axis::Horizontal),
};
let dest = CGPoint::new(current_x as f64, current_y as f64);
let Ok(event) =
CGEvent::new_mouse_event(self.event_source.clone(), event_type, dest, button)
else {
return Err(InputError::Simulate(
"failed creating event to enter mouse button",
));
};
event.set_integer_value_field(EventField::MOUSE_EVENT_CLICK_STATE, click_count);
event.post(CGEventTapLocation::HID);
}
if direction == Direction::Click || direction == Direction::Release {
let click_count = self.nth_button_press(button, Direction::Release);
let (button, event_type) = match button {
Button::Left => (CGMouseButton::Left, CGEventType::LeftMouseUp),
Button::Middle => (CGMouseButton::Center, CGEventType::OtherMouseUp),
Button::Right => (CGMouseButton::Right, CGEventType::RightMouseUp),
Button::ScrollUp
| Button::ScrollDown
| Button::ScrollLeft
| Button::ScrollRight => {
info!("On macOS the mouse_up function has no effect when called with one of the Scroll buttons");
return Ok(());
}
};
let dest = CGPoint::new(current_x as f64, current_y as f64);
let Ok(event) =
CGEvent::new_mouse_event(self.event_source.clone(), event_type, dest, button)
else {
return Err(InputError::Simulate(
"failed creating event to enter mouse button",
));
};
event.set_integer_value_field(EventField::MOUSE_EVENT_CLICK_STATE, click_count);
event.post(CGEventTapLocation::HID);
}
Ok(())
}
fn move_mouse(&mut self, x: i32, y: i32, coordinate: Coordinate) -> InputResult<()> {
debug!("\x1b[93mmove_mouse(x: {x:?}, y: {y:?}, coordinate:{coordinate:?})\x1b[0m");
let pressed = Self::pressed_buttons()?;
let (current_x, current_y) = self.location()?;
let (absolute, relative) = match coordinate {
// TODO: Check the bounds
Coordinate::Abs => ((x, y), (current_x - x, current_y - y)),
Coordinate::Rel => ((current_x + x, current_y + y), (x, y)),
};
let (event_type, button) = if pressed & 1 > 0 {
(CGEventType::LeftMouseDragged, CGMouseButton::Left)
} else if pressed & 2 > 0 {
(CGEventType::RightMouseDragged, CGMouseButton::Right)
} else {
(CGEventType::MouseMoved, CGMouseButton::Left) // The mouse button here is ignored so it can be anything
};
let dest = CGPoint::new(absolute.0 as f64, absolute.1 as f64);
let Ok(event) =
CGEvent::new_mouse_event(self.event_source.clone(), event_type, dest, button)
else {
return Err(InputError::Simulate(
"failed creating event to move the mouse",
));
};
// Add information by how much the mouse was moved
event.set_integer_value_field(
core_graphics::event::EventField::MOUSE_EVENT_DELTA_X,
relative.0.into(),
);
event.set_integer_value_field(
core_graphics::event::EventField::MOUSE_EVENT_DELTA_Y,
relative.1.into(),
);
event.post(CGEventTapLocation::HID);
Ok(())
}
// Sends a scroll event to the X11 server via `XTest` extension
fn scroll(&mut self, length: i32, axis: Axis) -> InputResult<()> {
debug!("\x1b[93mscroll(length: {length:?}, axis: {axis:?})\x1b[0m");
let (ax, len_x, len_y) = match axis {
Axis::Horizontal => (2, 0, -length),
Axis::Vertical => (1, -length, 0),
};
let Ok(event) = CGEvent::new_scroll_event(
self.event_source.clone(),
ScrollEventUnit::LINE,
ax,
len_x,
len_y,
0,
) else {
return Err(InputError::Simulate("failed creating event to scroll"));
};
event.post(CGEventTapLocation::HID);
Ok(())
}
fn main_display(&self) -> InputResult<(i32, i32)> {
debug!("\x1b[93mmain_display()\x1b[0m");
Ok((
self.display.pixels_wide() as i32,
self.display.pixels_high() as i32,
))
}
fn location(&self) -> InputResult<(i32, i32)> {
debug!("\x1b[93mlocation()\x1b[0m");
let pt = unsafe { AppKit::NSEvent::mouseLocation() };
let (x, y_inv) = (pt.x as i32, pt.y as i32);
Ok((x, self.display.pixels_high() as i32 - y_inv))
}
}
// https://stackoverflow.com/questions/1918841/how-to-convert-ascii-character-to-cgkeycode
impl Keyboard for Enigo {
fn fast_text(&mut self, text: &str) -> InputResult<Option<()>> {
// Fn to create an iterator over sub slices of a str that have the specified
// length
fn chunks(s: &str, len: usize) -> impl Iterator<Item = &str> {
assert!(len > 0);
let mut indices = s.char_indices().map(|(idx, _)| idx).peekable();
std::iter::from_fn(move || {
let Some(start_idx) = indices.next() else {
return None;
};
for _ in 0..len - 1 {
indices.next();
}
let end_idx = match indices.peek() {
Some(idx) => *idx,
None => s.bytes().len(),
};
Some(&s[start_idx..end_idx])
})
}
debug!("\x1b[93mfast_text(text: {text})\x1b[0m");
// NOTE(dustin): This is a fix for issue https://github.com/enigo-rs/enigo/issues/68
// The CGEventKeyboardSetUnicodeString function (used inside of
// event.set_string(chunk)) truncates strings down to 20 characters
for chunk in chunks(text, 20) {
let Ok(event) = CGEvent::new_keyboard_event(self.event_source.clone(), 0, true) else {
return Err(InputError::Simulate(
"failed creating event to enter the text",
));
};
event.set_string(chunk);
event.post(CGEventTapLocation::HID);
}
thread::sleep(Duration::from_millis(2));
Ok(Some(()))
}
fn key(&mut self, key: Key, direction: Direction) -> InputResult<()> {
debug!("\x1b[93mkey(key: {key:?}, direction: {direction:?})\x1b[0m");
// Nothing to do
if key == Key::Unicode('\0') {
return Ok(());
}
let Ok(keycode) = CGKeyCode::try_from(key) else {
return Err(InputError::InvalidInput(
"virtual keycodes on macOS have to fit into u16",
));
};
self.raw(keycode, direction)?;
// TODO: The list of keys will contain the key and also the associated keycode.
// They are a duplicate
match direction {
Direction::Press => {
debug!("added the key {key:?} to the held keys");
self.held.0.push(key);
}
Direction::Release => {
debug!("removed the key {key:?} from the held keys");
self.held.0.retain(|&k| k != key);
}
Direction::Click => (),
}
Ok(())
}
fn raw(&mut self, keycode: u16, direction: Direction) -> InputResult<()> {
debug!("\x1b[93mraw(keycode: {keycode:?}, direction: {direction:?})\x1b[0m");
if direction == Direction::Click || direction == Direction::Press {
thread::sleep(Duration::from_millis(self.delay));
let Ok(event) = CGEvent::new_keyboard_event(self.event_source.clone(), keycode, true)
else {
return Err(InputError::Simulate(
"failed creating event to press the key",
));
};
event.post(CGEventTapLocation::HID);
}
if direction == Direction::Click || direction == Direction::Release {
thread::sleep(Duration::from_millis(self.delay));
let Ok(event) = CGEvent::new_keyboard_event(self.event_source.clone(), keycode, false)
else {
return Err(InputError::Simulate(
"failed creating event to release the key",
));
};
event.post(CGEventTapLocation::HID);
}
match direction {
Direction::Press => {
debug!("added the keycode {keycode:?} to the held keys");
self.held.1.push(keycode);
}
Direction::Release => {
debug!("removed the keycode {keycode:?} from the held keys");
self.held.1.retain(|&k| k != keycode);
}
Direction::Click => (),
}
Ok(())
}
}
impl Enigo {
/// Create a new Enigo struct to establish the connection to simulate input
/// with the specified settings
///
/// # Errors
/// Have a look at the documentation of `NewConError` to see under which
/// conditions an error will be returned.
pub fn new(settings: &Settings) -> Result<Self, NewConError> {
let Settings {
mac_delay: delay,
release_keys_when_dropped,
..
} = settings;
let held = (Vec::new(), Vec::new());
let double_click_delay = Duration::from_secs(1);
let double_click_delay_setting = unsafe { AppKit::NSEvent::doubleClickInterval() };
// Returns the double click interval (https://developer.apple.com/documentation/appkit/nsevent/1528384-doubleclickinterval). This is a TimeInterval which is a f64 of the number of seconds
let double_click_delay = double_click_delay.mul_f64(double_click_delay_setting);
let Ok(event_source) = CGEventSource::new(CGEventSourceStateID::CombinedSessionState)
else {
return Err(NewConError::EstablishCon("failed creating event source"));
};
debug!("\x1b[93mconnection established on macOS\x1b[0m");
Ok(Enigo {
delay: (*delay).into(),
event_source,
display: CGDisplay::main(),
held,
release_keys_when_dropped: *release_keys_when_dropped,
double_click_delay,
last_mouse_click: [(0, Instant::now()); 7],
})
}
/// Get the delay per keypress in milliseconds
#[must_use]
#[allow(clippy::missing_panics_doc)] // It never panics
pub fn delay(&self) -> u32 {
self.delay.try_into().unwrap_or(u32::MAX)
}
/// Set the delay per keypress in milliseconds
pub fn set_delay(&mut self, delay: u32) {
self.delay = delay.into();
}
/// Returns a list of all currently pressed keys
pub fn held(&mut self) -> (Vec<Key>, Vec<CGKeyCode>) {
self.held.clone()
}
#[allow(clippy::unnecessary_wraps)]
fn pressed_buttons() -> InputResult<usize> {
let pressed_buttons = unsafe { AppKit::NSEvent::pressedMouseButtons() };
Ok(pressed_buttons)
}
// On macOS, we have to determine ourselves if it was a double click of a mouse
// button. The Enigo struct stores the information needed to do so. This
// function checks if the button was pressed down again fast enough to issue a
// double (or nth) click and returns the nth click it was. It also takes care of
// updating the information the Enigo struct stores.
fn nth_button_press(&mut self, button: Button, direction: Direction) -> i64 {
if direction == Direction::Press {
let last_time = self.last_mouse_click[button as usize].1;
self.last_mouse_click[button as usize].1 = Instant::now();
if last_time.elapsed() < self.double_click_delay {
self.last_mouse_click[button as usize].0 += 1;
} else {
self.last_mouse_click[button as usize].0 = 1;
}
}
let nth_button_press = self.last_mouse_click[button as usize].0;
debug!("nth_button_press: {nth_button_press}");
nth_button_press
}
}
/// Converts a `Key` to a `CGKeyCode`
impl TryFrom<Key> for core_graphics::event::CGKeyCode {
type Error = ();
#[allow(clippy::too_many_lines)]
fn try_from(key: Key) -> Result<Self, Self::Error> {
// A list of names is available at:
// https://docs.rs/core-graphics/latest/core_graphics/event/struct.KeyCode.html
// https://github.com/phracker/MacOSX-SDKs/blob/master/MacOSX10.13.sdk/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/Headers/Events.h
let key = match key {
Key::Alt | Key::Option => KeyCode::OPTION,
Key::Backspace => KeyCode::DELETE,
Key::CapsLock => KeyCode::CAPS_LOCK,
Key::Control | Key::LControl => KeyCode::CONTROL,
Key::Delete => KeyCode::FORWARD_DELETE,
Key::DownArrow => KeyCode::DOWN_ARROW,
Key::End => KeyCode::END,
Key::Escape => KeyCode::ESCAPE,
Key::F1 => KeyCode::F1,
Key::F2 => KeyCode::F2,
Key::F3 => KeyCode::F3,
Key::F4 => KeyCode::F4,
Key::F5 => KeyCode::F5,
Key::F6 => KeyCode::F6,
Key::F7 => KeyCode::F7,
Key::F8 => KeyCode::F8,
Key::F9 => KeyCode::F9,
Key::F10 => KeyCode::F10,
Key::F11 => KeyCode::F11,
Key::F12 => KeyCode::F12,
Key::F13 => KeyCode::F13,
Key::F14 => KeyCode::F14,
Key::F15 => KeyCode::F15,
Key::F16 => KeyCode::F16,
Key::F17 => KeyCode::F17,
Key::F18 => KeyCode::F18,
Key::F19 => KeyCode::F19,
Key::F20 => KeyCode::F20,
Key::Function => KeyCode::FUNCTION,
Key::Help => KeyCode::HELP,
Key::Home => KeyCode::HOME,
Key::Launchpad => 160,
Key::LeftArrow => KeyCode::LEFT_ARROW,
Key::MissionControl => 131,
Key::PageDown => KeyCode::PAGE_DOWN,
Key::PageUp => KeyCode::PAGE_UP,
Key::RCommand => KeyCode::RIGHT_COMMAND,
Key::RControl => KeyCode::RIGHT_CONTROL,
Key::Return => KeyCode::RETURN,
Key::RightArrow => KeyCode::RIGHT_ARROW,
Key::RShift => KeyCode::RIGHT_SHIFT,
Key::ROption => KeyCode::RIGHT_OPTION,
Key::Shift | Key::LShift => KeyCode::SHIFT,
Key::Space => KeyCode::SPACE,
Key::Tab => KeyCode::TAB,
Key::UpArrow => KeyCode::UP_ARROW,
Key::VolumeDown => KeyCode::VOLUME_DOWN,
Key::VolumeUp => KeyCode::VOLUME_UP,
Key::VolumeMute => KeyCode::MUTE,
Key::Unicode(c) => get_layoutdependent_keycode(&c.to_string()),
Key::Other(v) => {
let Ok(v) = u16::try_from(v) else {
return Err(());
};
v
}
Key::Super | Key::Command | Key::Windows | Key::Meta => KeyCode::COMMAND,
};
Ok(key)
}
}
fn get_layoutdependent_keycode(string: &str) -> CGKeyCode {
let mut pressed_keycode = 0;
// loop through every keycode (0 - 127)
for keycode in 0..128 {
// no modifier
if let Some(key_string) = keycode_to_string(keycode, 0x100) {
// debug!("{:?}", string);
if string == key_string {
pressed_keycode = keycode;
}
}
// shift modifier
if let Some(key_string) = keycode_to_string(keycode, 0x20102) {
// debug!("{:?}", string);
if string == key_string {
pressed_keycode = keycode;
}
}
// alt modifier
// if let Some(string) = keycode_to_string(keycode, 0x80120) {
// debug!("{:?}", string);
// }
// alt + shift modifier
// if let Some(string) = keycode_to_string(keycode, 0xa0122) {
// debug!("{:?}", string);
// }
}
pressed_keycode
}
fn keycode_to_string(keycode: u16, modifier: u32) -> Option<String> {
let cf_string = create_string_for_key(keycode, modifier);
let buffer_size = unsafe { CFStringGetLength(cf_string) + 1 };
let mut buffer: i8 = std::i8::MAX;
let success =
unsafe { CFStringGetCString(cf_string, &mut buffer, buffer_size, kCFStringEncodingUTF8) };
if success == TRUE as u8 {
let rust_string = String::from_utf8(vec![buffer as u8]).unwrap();
Some(rust_string)
} else {
None
}
}
#[allow(clippy::unused_self)]
fn create_string_for_key(keycode: u16, modifier: u32) -> CFStringRef {
let mut current_keyboard = unsafe { TISCopyCurrentKeyboardInputSource() };
let mut layout_data =
unsafe { TISGetInputSourceProperty(current_keyboard, kTISPropertyUnicodeKeyLayoutData) };
if layout_data.is_null() {
debug!("TISGetInputSourceProperty(current_keyboard, kTISPropertyUnicodeKeyLayoutData) returned 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());
}
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 real_length: UniCharCount = 0;
unsafe {
UCKeyTranslate(
keyboard_layout,
keycode,
3, // kUCKeyActionDisplay = 3
modifier,
LMGetKbdType() as u32,
kUCKeyTranslateNoDeadKeysBit,
&mut keys_down,
8, // sizeof(chars) / sizeof(chars[0]),
&mut real_length,
&mut chars,
);
}
unsafe { CFStringCreateWithCharacters(kCFAllocatorDefault, &chars, 1) }
}
impl Drop for Enigo {
// Release the held keys before the connection is dropped
fn drop(&mut self) {
if !self.release_keys_when_dropped {
return;
}
let (held_keys, held_keycodes) = self.held();
for key in held_keys {
if self.key(key, Direction::Release).is_err() {
error!("unable to release {key:?}");
};
}
for keycode in held_keycodes {
if self.raw(keycode, Direction::Release).is_err() {
error!("unable to release {keycode:?}");
};
}
debug!("released all held keys");
}
}