enigo 0.6.1

Cross-platform (Linux, Windows, macOS & BSD) library to simulate keyboard and mouse events
Documentation
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
use std::os::raw::c_void;
use std::{
    thread,
    time::{Duration, Instant},
};

use core_foundation::{
    array::CFIndex,
    base::{CFRelease, OSStatus, TCFType, UInt8, UInt16, UInt32},
    data::{CFDataGetBytePtr, CFDataRef},
    dictionary::{CFDictionary, CFDictionaryRef},
    string::{CFString, CFStringRef, UniChar},
};
use core_graphics::{
    display::{CGDisplay, CGPoint},
    event::{
        CGEvent, CGEventFlags, CGEventRef, CGEventTapLocation, CGEventType, CGKeyCode,
        CGMouseButton, CGScrollEventUnit, EventField, KeyCode, ScrollEventUnit,
    },
    event_source::{CGEventSource, CGEventSourceStateID},
};
use foreign_types_shared::ForeignTypeRef as _;
use log::{debug, error, info};
use objc2::msg_send;
use objc2_app_kit::{NSEvent, NSEventModifierFlags, NSEventType};
use objc2_foundation::NSPoint;

use crate::{
    Axis, Button, Coordinate, Direction, InputError, InputResult, Key, Keyboard, Mouse,
    NewConError, Settings,
};

#[repr(C)]
struct __TISInputSource;
type TISInputSourceRef = *const __TISInputSource;

#[allow(non_upper_case_globals)]
const kUCKeyTranslateNoDeadKeysBit: CFIndex = 0; // Previously was always u32. Change it back if there are bugs

#[allow(improper_ctypes)]
#[link(name = "Carbon", kind = "framework")]
unsafe extern "C" {
    // “Copy” here means +1 retain — we must CFRelease when done
    fn TISCopyCurrentKeyboardInputSource() -> TISInputSourceRef;
    fn TISCopyCurrentKeyboardLayoutInputSource() -> TISInputSourceRef;
    fn TISCopyCurrentASCIICapableKeyboardLayoutInputSource() -> TISInputSourceRef;

    // property key for the Unicode (‘uchr’) layout data
    #[allow(non_upper_case_globals)]
    static kTISPropertyUnicodeKeyLayoutData: CFStringRef;

    // fetches a CFDataRef containing the raw UCKeyboardLayout bytes
    #[allow(non_snake_case)]
    fn TISGetInputSourceProperty(
        inputSource: TISInputSourceRef,
        propertyKey: CFStringRef,
    ) -> CFDataRef;

    // turn keycode+modifiers → UTF‑16 string
    #[allow(non_snake_case)]
    fn UCKeyTranslate(
        keyLayoutPtr: *const UInt8, //*const UCKeyboardLayout,
        virtualKeyCode: UInt16,
        keyAction: UInt16,
        modifierKeyState: UInt32,
        keyboardType: UInt32,
        keyTranslateOptions: CFIndex,
        deadKeyState: *mut UInt32,
        maxStringLength: CFIndex,
        actualStringLength: *mut CFIndex,
        unicodeString: *mut UniChar,
    ) -> OSStatus;

    fn LMGetKbdType() -> UInt8;
}

/// The main struct for handling the event emitting
pub struct Enigo {
    event_source: CGEventSource,
    display: CGDisplay,
    held: (Vec<Key>, Vec<CGKeyCode>), // Currently held keys
    event_source_user_data: i64,
    release_keys_when_dropped: bool,
    event_flags: CGEventFlags,
    double_click_delay: Duration,
    // Instant when the last event was sent and the duration that needs to be waited for after that
    // instant to make sure all events were handled by the OS
    last_event: (Instant, Duration),
    // TODO: Use mem::variant_count::<Button>() here instead of 9 once it is stabilized
    last_mouse_click: [(i64, Instant); 9], /* For each of the nine 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 */
}

// TODO: Double check this is safe
unsafe impl Send for Enigo {}

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, button_number) = match button {
                Button::Left => (CGMouseButton::Left, CGEventType::LeftMouseDown, None),
                Button::Middle => (CGMouseButton::Center, CGEventType::OtherMouseDown, Some(2)),
                Button::Right => (CGMouseButton::Right, CGEventType::RightMouseDown, None),
                Button::Back => (CGMouseButton::Center, CGEventType::OtherMouseDown, Some(3)),
                Button::Forward => (CGMouseButton::Center, CGEventType::OtherMouseDown, Some(4)),
                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",
                ));
            };

            if let Some(button_number) = button_number {
                event.set_integer_value_field(EventField::MOUSE_EVENT_BUTTON_NUMBER, button_number);
            }
            event.set_integer_value_field(EventField::MOUSE_EVENT_CLICK_STATE, click_count);
            event.set_integer_value_field(
                EventField::EVENT_SOURCE_USER_DATA,
                self.event_source_user_data,
            );
            event.set_flags(self.event_flags);
            event.post(CGEventTapLocation::HID);
            self.update_wait_time();
        }
        if direction == Direction::Click || direction == Direction::Release {
            let click_count = self.nth_button_press(button, Direction::Release);
            let (button, event_type, button_number) = match button {
                Button::Left => (CGMouseButton::Left, CGEventType::LeftMouseUp, None),
                Button::Middle => (CGMouseButton::Center, CGEventType::OtherMouseUp, Some(2)),
                Button::Right => (CGMouseButton::Right, CGEventType::RightMouseUp, None),
                Button::Back => (CGMouseButton::Center, CGEventType::OtherMouseUp, Some(3)),
                Button::Forward => (CGMouseButton::Center, CGEventType::OtherMouseUp, Some(4)),
                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",
                ));
            };

            if let Some(button_number) = button_number {
                event.set_integer_value_field(EventField::MOUSE_EVENT_BUTTON_NUMBER, button_number);
            }
            event.set_integer_value_field(EventField::MOUSE_EVENT_CLICK_STATE, click_count);
            event.set_integer_value_field(
                EventField::EVENT_SOURCE_USER_DATA,
                self.event_source_user_data,
            );
            event.set_flags(self.event_flags);
            event.post(CGEventTapLocation::HID);
            self.update_wait_time();
        }
        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 = unsafe { NSEvent::pressedMouseButtons() };
        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.set_integer_value_field(
            EventField::EVENT_SOURCE_USER_DATA,
            self.event_source_user_data,
        );
        event.set_flags(self.event_flags);
        event.post(CGEventTapLocation::HID);
        self.update_wait_time();
        Ok(())
    }

    fn scroll(&mut self, length: i32, axis: Axis) -> InputResult<()> {
        debug!("\x1b[93mscroll(length: {length:?}, axis: {axis:?})\x1b[0m");
        self.scroll_unit(length, ScrollEventUnit::LINE, axis)
    }

    #[cfg(all(feature = "platform_specific", target_os = "macos"))]
    fn smooth_scroll(&mut self, length: i32, axis: Axis) -> InputResult<()> {
        debug!("\x1b[93msmooth_scroll(length: {length:?}, axis: {axis:?})\x1b[0m");
        self.scroll_unit(length, ScrollEventUnit::PIXEL, axis)
    }

    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 { 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 start_idx = indices.next()?;
                for _ in 0..len - 1 {
                    indices.next();
                }
                let end_idx = match indices.peek() {
                    Some(idx) => *idx,
                    None => s.len(),
                };
                Some(&s[start_idx..end_idx])
            })
        }

        debug!("\x1b[93mfast_text(text: {text})\x1b[0m");
        // WORKAROUND: 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 mut 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",
                ));
            };
            // WORKAROUND: This is a fix for issue https://github.com/enigo-rs/enigo/issues/260
            // This is needed to get rid of all leading line feed, tab and carriage return
            // characters. event.set_string(chunk)) silently fails if the chunk
            // starts with a newline character
            loop {
                if chunk.starts_with('\t') {
                    self.key(Key::Tab, Direction::Click)?;
                    chunk = &chunk[1..];
                    continue;
                }
                if chunk.starts_with('\r') {
                    self.fast_text("\u{200B}\r")?;
                    chunk = &chunk[1..];
                    continue;
                }
                if chunk.starts_with('\n') {
                    self.fast_text("\u{200B}\n")?;
                    chunk = &chunk[1..];
                    continue;
                }
                break;
            }

            event.set_string(chunk);
            event.set_integer_value_field(
                EventField::EVENT_SOURCE_USER_DATA,
                self.event_source_user_data,
            );
            // We want to ignore all modifiers when entering text
            event.set_flags(CGEventFlags::CGEventFlagNull);
            event.post(CGEventTapLocation::HID);
            self.update_wait_time();
        }
        Ok(Some(()))
    }

    #[allow(clippy::too_many_lines)]
    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(());
        }
        match key {
            Key::VolumeUp => {
                debug!("special case for handling the VolumeUp key");
                self.special_keys(0, direction)?;
            }
            Key::VolumeDown => {
                debug!("special case for handling the VolumeDown key");
                self.special_keys(1, direction)?;
            }
            Key::BrightnessUp => {
                debug!("special case for handling the BrightnessUp key");
                self.special_keys(2, direction)?;
            }
            Key::BrightnessDown => {
                debug!("special case for handling the BrightnessDown key");
                self.special_keys(3, direction)?;
            }
            Key::Power => {
                debug!("special case for handling the Power key");
                self.special_keys(6, direction)?;
            }
            Key::VolumeMute => {
                debug!("special case for handling the VolumeMute key");
                self.special_keys(7, direction)?;
            }

            Key::ContrastUp => {
                debug!("special case for handling the ContrastUp key");
                self.special_keys(11, direction)?;
            }
            Key::ContrastDown => {
                debug!("special case for handling the ContrastDown key");
                self.special_keys(12, direction)?;
            }
            Key::LaunchPanel => {
                debug!("special case for handling the LaunchPanel key");
                self.special_keys(13, direction)?;
            }
            Key::Eject => {
                debug!("special case for handling the Eject key");
                self.special_keys(14, direction)?;
            }
            Key::VidMirror => {
                debug!("special case for handling the VidMirror key");
                self.special_keys(15, direction)?;
            }
            Key::MediaPlayPause => {
                debug!("special case for handling the MediaPlayPause key");
                self.special_keys(16, direction)?;
            }
            Key::MediaNextTrack => {
                debug!("special case for handling the MediaNextTrack key");
                self.special_keys(17, direction)?;
            }
            Key::MediaPrevTrack => {
                debug!("special case for handling the MediaPrevTrack key");
                self.special_keys(18, direction)?;
            }
            Key::MediaFast => {
                debug!("special case for handling the MediaFast key");
                self.special_keys(19, direction)?;
            }
            Key::MediaRewind => {
                debug!("special case for handling the MediaRewind key");
                self.special_keys(20, direction)?;
            }
            Key::IlluminationUp => {
                debug!("special case for handling the IlluminationUp key");
                self.special_keys(21, direction)?;
            }
            Key::IlluminationDown => {
                debug!("special case for handling the IlluminationDown key");
                self.special_keys(22, direction)?;
            }
            Key::IlluminationToggle => {
                debug!("special case for handling the IlluminationToggle key");
                self.special_keys(23, direction)?;
            }
            _ => {
                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 {
            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.set_integer_value_field(
                EventField::EVENT_SOURCE_USER_DATA,
                self.event_source_user_data,
            );
            self.add_event_flag(keycode, Direction::Press);
            event.set_flags(self.event_flags);
            event.post(CGEventTapLocation::HID);
            self.update_wait_time();
        }

        if direction == Direction::Click || direction == Direction::Release {
            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.set_integer_value_field(
                EventField::EVENT_SOURCE_USER_DATA,
                self.event_source_user_data,
            );
            self.add_event_flag(keycode, Direction::Release);
            event.set_flags(self.event_flags);
            event.post(CGEventTapLocation::HID);
            self.update_wait_time();
        }

        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 {
            release_keys_when_dropped,
            event_source_user_data,
            open_prompt_to_get_permissions,
            independent_of_keyboard_state,
            ..
        } = settings;

        if !has_permission(*open_prompt_to_get_permissions) {
            error!("The application does not have the permission to simulate input!");
            return Err(NewConError::NoPermission);
        }
        info!("The application has the permission to simulate input");

        let held = (Vec::new(), Vec::new());

        let mut event_flags = CGEventFlags::CGEventFlagNonCoalesced;
        event_flags.set(CGEventFlags::from_bits_retain(0x2000_0000), true); // I don't know if this is needed or what this flag does. Correct events have it
        // set so we also do it (until we know it is wrong)

        let double_click_delay = Duration::from_secs(1);
        let double_click_delay_setting = unsafe { 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 event_source_state = if *independent_of_keyboard_state {
            CGEventSourceStateID::Private
        } else {
            CGEventSourceStateID::CombinedSessionState
        };
        let Ok(event_source) = CGEventSource::new(event_source_state) else {
            return Err(NewConError::EstablishCon("failed creating event source"));
        };

        debug!("\x1b[93mconnection established on macOS\x1b[0m");

        let last_event = (Instant::now(), Duration::from_secs(0));
        Ok(Enigo {
            event_source,
            display: CGDisplay::main(),
            held,
            release_keys_when_dropped: *release_keys_when_dropped,
            event_flags,
            double_click_delay,
            last_event,
            last_mouse_click: [(0, Instant::now()); 9],
            event_source_user_data: event_source_user_data.unwrap_or(crate::EVENT_MARKER as i64),
        })
    }

    /// Returns a list of all currently pressed keys
    pub fn held(&mut self) -> (Vec<Key>, Vec<CGKeyCode>) {
        self.held.clone()
    }

    /// Returns the value that enigo's events are marked with
    #[must_use]
    pub fn get_marker_value(&self) -> i64 {
        self.event_source_user_data
    }

    // 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
    }

    fn special_keys(&mut self, code: isize, direction: Direction) -> InputResult<()> {
        if direction == Direction::Press || direction == Direction::Click {
            let event = unsafe {
                NSEvent::otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2(
                NSEventType::SystemDefined, // 14
                NSPoint::ZERO,
                NSEventModifierFlags::empty(),
                0.0,
                0,
                None,
                8,
                (code << 16) | (0xa << 8),
                -1
            )
            };

            if let Some(event) = event {
                let cg_event = unsafe { Self::ns_event_cg_event(&event).to_owned() };
                cg_event.set_integer_value_field(
                    EventField::EVENT_SOURCE_USER_DATA,
                    self.event_source_user_data,
                );
                cg_event.set_flags(self.event_flags);
                cg_event.post(CGEventTapLocation::HID);
                self.update_wait_time();
            } else {
                return Err(InputError::Simulate(
                    "failed creating event to press special key",
                ));
            }
        }

        if direction == Direction::Release || direction == Direction::Click {
            let event = unsafe {
                NSEvent::otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2(
                    NSEventType::SystemDefined, // 14
                NSPoint::ZERO,
                NSEventModifierFlags::empty(),
                0.0,
                0,
                None,
                8,
                (code << 16) | (0xb << 8),
                -1
            )
            };

            if let Some(event) = event {
                let cg_event = unsafe { Self::ns_event_cg_event(&event).to_owned() };
                cg_event.set_integer_value_field(
                    EventField::EVENT_SOURCE_USER_DATA,
                    self.event_source_user_data,
                );
                cg_event.set_flags(self.event_flags);
                cg_event.post(CGEventTapLocation::HID);
                self.update_wait_time();
            } else {
                return Err(InputError::Simulate(
                    "failed creating event to release special key",
                ));
            }
        }

        Ok(())
    }

    unsafe fn ns_event_cg_event(event: &NSEvent) -> &CGEventRef {
        let ptr: *mut c_void = unsafe { msg_send![event, CGEvent] };
        unsafe { CGEventRef::from_ptr(ptr.cast()) }
    }

    // TODO: Remove this once the values for KeyCode were upstreamed: https://github.com/servo/core-foundation-rs/pull/712
    #[allow(clippy::match_same_arms)]
    #[allow(clippy::too_many_lines)]
    /// Adds or removes `KeyFlags` as needed by the keycode
    ///
    /// This function can never get called with `Direction::Click`!
    fn add_event_flag(&mut self, keycode: CGKeyCode, direction: Direction) {
        // Upstream these to https://github.com/servo/core-foundation-rs
        const NX_DEVICELCTLKEYMASK: CGEventFlags = CGEventFlags::from_bits_retain(0x0000_0001);
        const NX_DEVICELSHIFTKEYMASK: CGEventFlags = CGEventFlags::from_bits_retain(0x0000_0002);
        const NX_DEVICERSHIFTKEYMASK: CGEventFlags = CGEventFlags::from_bits_retain(0x0000_0004);
        const NX_DEVICELCMDKEYMASK: CGEventFlags = CGEventFlags::from_bits_retain(0x0000_0008);
        const NX_DEVICERCMDKEYMASK: CGEventFlags = CGEventFlags::from_bits_retain(0x0000_0010);
        const NX_DEVICELALTKEYMASK: CGEventFlags = CGEventFlags::from_bits_retain(0x0000_0020);
        const NX_DEVICERALTKEYMASK: CGEventFlags = CGEventFlags::from_bits_retain(0x0000_0040);
        const NX_DEVICE_ALPHASHIFT_STATELESS_MASK: CGEventFlags =
            CGEventFlags::from_bits_retain(0x0000_0080);
        const NX_DEVICERCTLKEYMASK: CGEventFlags = CGEventFlags::from_bits_retain(0x0000_2000);

        type FlagOp = fn(&mut CGEventFlags, CGEventFlags);

        fn no_op(_: &mut CGEventFlags, _: CGEventFlags) {}

        // These flags have been determined by entering all keys with the previous
        // implementation that does not set the flags manually and checking the
        // resulting flags in their events. Some of the keys set the EventFlag even when
        // they are released. It's a bit weird, but for now we just copy the behavior
        // here
        let (press_fn, release_fn, event_flag): (FlagOp, FlagOp, CGEventFlags) = match keycode {
            KeyCode::RIGHT_COMMAND => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagCommand | NX_DEVICERCMDKEYMASK,
            ),
            KeyCode::COMMAND => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagCommand | NX_DEVICELCMDKEYMASK,
            ),
            KeyCode::SHIFT => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagShift | NX_DEVICELSHIFTKEYMASK,
            ),
            KeyCode::CAPS_LOCK => (
                CGEventFlags::toggle,
                no_op,
                CGEventFlags::CGEventFlagAlphaShift | NX_DEVICE_ALPHASHIFT_STATELESS_MASK, /* TODO: The NX_DEVICE_ALPHASHIFT_STATELESS_MASK did not get set when simulating CapsLock with the old implementation, but I'll go out on a limb and set it anyway. */
            ),
            KeyCode::OPTION => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagAlternate | NX_DEVICELALTKEYMASK,
            ),
            KeyCode::CONTROL => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagControl | NX_DEVICELCTLKEYMASK,
            ),
            KeyCode::RIGHT_SHIFT => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagShift | NX_DEVICERSHIFTKEYMASK,
            ),
            KeyCode::RIGHT_OPTION => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagAlternate | NX_DEVICERALTKEYMASK,
            ),
            KeyCode::RIGHT_CONTROL => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagControl | NX_DEVICERCTLKEYMASK,
            ),
            KeyCode::FUNCTION => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagSecondaryFn,
            ),
            KeyCode::F17 => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagSecondaryFn,
            ),
            KeyCode::ANSI_KEYPAD_DECIMAL => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagNumericPad,
            ),
            KeyCode::ANSI_KEYPAD_MULTIPLY => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagNumericPad,
            ),
            KeyCode::ANSI_KEYPAD_PLUS => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagNumericPad,
            ),
            KeyCode::ANSI_KEYPAD_CLEAR => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagSecondaryFn,
            ),
            KeyCode::ANSI_KEYPAD_DIVIDE => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagNumericPad,
            ),
            KeyCode::ANSI_KEYPAD_ENTER => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagNumericPad,
            ),
            KeyCode::ANSI_KEYPAD_MINUS => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagNumericPad,
            ),
            KeyCode::F18 => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagSecondaryFn,
            ),
            KeyCode::F19 => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagSecondaryFn,
            ),
            KeyCode::ANSI_KEYPAD_EQUAL..=KeyCode::ANSI_KEYPAD_7 => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagNumericPad,
            ),
            KeyCode::ANSI_KEYPAD_8..=KeyCode::ANSI_KEYPAD_9 => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagNumericPad,
            ),
            KeyCode::F5..=KeyCode::F9 => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagSecondaryFn,
            ),
            KeyCode::F11 => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagSecondaryFn,
            ),
            KeyCode::F13..=KeyCode::F14 => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagSecondaryFn,
            ),
            KeyCode::F10 => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagSecondaryFn,
            ),
            KeyCode::F12 => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagSecondaryFn,
            ),
            KeyCode::F15 => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagSecondaryFn,
            ),
            KeyCode::HELP => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagSecondaryFn | CGEventFlags::CGEventFlagHelp,
            ),
            KeyCode::HOME..KeyCode::LEFT_ARROW => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagSecondaryFn,
            ),
            KeyCode::LEFT_ARROW..0x7f => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagSecondaryFn | CGEventFlags::CGEventFlagNumericPad,
            ),
            0x81..0x84 => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagSecondaryFn,
            ),
            0x90 => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagSecondaryFn,
            ),
            0x91 => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagSecondaryFn,
            ),
            0xa0 => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagSecondaryFn,
            ),
            0xb0..0xb3 => (
                CGEventFlags::insert,
                CGEventFlags::remove,
                CGEventFlags::CGEventFlagSecondaryFn,
            ),
            _ => (no_op, no_op, CGEventFlags::CGEventFlagNull),
        };

        let flag_fn = match direction {
            Direction::Click => {
                unreachable!(
                    "The function should never get called with Direction::Click. If it was, it's an implementation error"
                );
            }
            Direction::Press => press_fn,
            Direction::Release => release_fn,
        };

        flag_fn(&mut self.event_flags, event_flag);
    }

    fn scroll_unit(
        &mut self,
        length: i32,
        scroll_event_unit: CGScrollEventUnit,
        axis: Axis,
    ) -> InputResult<()> {
        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(),
            scroll_event_unit,
            ax,
            len_x,
            len_y,
            0,
        ) else {
            return Err(InputError::Simulate("failed creating event to scroll"));
        };

        event.set_integer_value_field(
            EventField::EVENT_SOURCE_USER_DATA,
            self.event_source_user_data,
        );
        event.set_flags(self.event_flags);
        event.post(CGEventTapLocation::HID);
        self.update_wait_time();
        Ok(())
    }

    /// Save the current Instant and calculate the remaining waiting time
    /// We assume we need to wait for 20 ms for each event to make sure the OS
    /// has time to handle it. Instead of simply adding 20 ms for each event, we
    /// assume that the OS handled events between us sending events. That's why
    /// we subtract the time we already waited between events.
    fn update_wait_time(&mut self) {
        let now = Instant::now();
        let wait_time = self
            .last_event
            .1
            .saturating_sub(self.last_event.0.elapsed())
            + Duration::from_millis(20);
        self.last_event = (now, wait_time);
    }
}

/// 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::Add => KeyCode::ANSI_KEYPAD_PLUS,
            Key::Alt | Key::Option => KeyCode::OPTION,
            Key::Backspace => KeyCode::DELETE,
            Key::CapsLock => KeyCode::CAPS_LOCK,
            Key::Control | Key::LControl => KeyCode::CONTROL,
            Key::Decimal => KeyCode::ANSI_KEYPAD_DECIMAL,
            Key::Delete => KeyCode::FORWARD_DELETE,
            Key::Divide => KeyCode::ANSI_KEYPAD_DIVIDE,
            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 => 131,
            Key::LeftArrow => KeyCode::LEFT_ARROW,
            Key::MissionControl => 160,
            Key::Multiply => KeyCode::ANSI_KEYPAD_MULTIPLY,
            Key::Numpad0 => KeyCode::ANSI_KEYPAD_0,
            Key::Numpad1 => KeyCode::ANSI_KEYPAD_1,
            Key::Numpad2 => KeyCode::ANSI_KEYPAD_2,
            Key::Numpad3 => KeyCode::ANSI_KEYPAD_3,
            Key::Numpad4 => KeyCode::ANSI_KEYPAD_4,
            Key::Numpad5 => KeyCode::ANSI_KEYPAD_5,
            Key::Numpad6 => KeyCode::ANSI_KEYPAD_6,
            Key::Numpad7 => KeyCode::ANSI_KEYPAD_7,
            Key::Numpad8 => KeyCode::ANSI_KEYPAD_8,
            Key::Numpad9 => KeyCode::ANSI_KEYPAD_9,
            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::Subtract => KeyCode::ANSI_KEYPAD_MINUS,
            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,
            Key::BrightnessDown
            | Key::BrightnessUp
            | Key::ContrastUp
            | Key::ContrastDown
            | Key::Eject
            | Key::IlluminationDown
            | Key::IlluminationUp
            | Key::IlluminationToggle
            | Key::LaunchPanel
            | Key::MediaFast
            | Key::MediaNextTrack
            | Key::MediaPlayPause
            | Key::MediaPrevTrack
            | Key::MediaRewind
            | Key::Power
            | Key::VidMirror => return Err(()),
        };
        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 Ok(key_string) = keycode_to_string(keycode, 0x100) {
            // debug!("{:?}", string);
            if string == key_string {
                pressed_keycode = keycode;
            }
        }

        // shift modifier
        if let Ok(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) -> Result<String, String> {
    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"
        );
        unsafe { CFRelease(current_keyboard.cast::<c_void>()) };

        // 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)
        };
        if layout_data.is_null() {
            debug!(
                "TISGetInputSourceProperty(current_keyboard, kTISPropertyUnicodeKeyLayoutData) returned NULL again"
            );
            unsafe { CFRelease(current_keyboard.cast::<c_void>()) };
            current_keyboard = unsafe { TISCopyCurrentASCIICapableKeyboardLayoutInputSource() };
            layout_data = unsafe {
                TISGetInputSourceProperty(current_keyboard, kTISPropertyUnicodeKeyLayoutData)
            };
            debug_assert!(!layout_data.is_null());
            debug!("Using layout of the TISCopyCurrentASCIICapableKeyboardLayoutInputSource");
        }
    }

    let keyboard_layout = unsafe { CFDataGetBytePtr(layout_data) };

    let mut keys_down: UInt32 = 0;
    let mut chars: [UniChar; 1] = [0];
    let mut real_length = 0;
    let status = unsafe {
        UCKeyTranslate(
            keyboard_layout,
            keycode,
            3, // kUCKeyActionDisplay = 3
            modifier,
            LMGetKbdType() as u32,
            kUCKeyTranslateNoDeadKeysBit,
            &raw mut keys_down,
            chars.len() as CFIndex,
            &raw mut real_length,
            chars.as_mut_ptr(),
        )
    };
    unsafe { CFRelease(current_keyboard.cast::<c_void>()) };

    if status != 0 {
        error!("UCKeyTranslate failed with status: {status}");
        return Err(format!("OSStatus error: {status}"));
    }

    let utf16_slice = &chars[..real_length as usize];
    String::from_utf16(utf16_slice).map_err(|e| {
        error!("UTF-16 to String converstion failed: {e:?}");
        format!("FromUtf16Error: {e}")
    })
}

#[link(name = "ApplicationServices", kind = "framework")]
unsafe extern "C" {
    pub fn AXIsProcessTrustedWithOptions(options: CFDictionaryRef) -> bool;
    static kAXTrustedCheckOptionPrompt: CFStringRef;
}

/// Check if the currently running application has the permissions to simulate
/// input
///
/// Returns true if the application has the permission and is allowed to
/// simulate input
pub fn has_permission(open_prompt_to_get_permissions: bool) -> bool {
    let key = unsafe { kAXTrustedCheckOptionPrompt };
    let key = unsafe { CFString::wrap_under_create_rule(key) };

    let value = if open_prompt_to_get_permissions {
        debug!("Open the system prompt if the permissions are missing.");
        core_foundation::boolean::CFBoolean::true_value()
    } else {
        debug!("Do not open the system prompt if the permissions are missing.");
        core_foundation::boolean::CFBoolean::false_value()
    };

    let options = CFDictionary::from_CFType_pairs(&[(key, value)]);
    let options = options.as_concrete_TypeRef();
    unsafe { AXIsProcessTrustedWithOptions(options) }
}

impl Drop for Enigo {
    // Release the held keys before the connection is dropped
    fn drop(&mut self) {
        if self.release_keys_when_dropped {
            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");
        }

        // DO NOT REMOVE THE SLEEP
        // This sleep is needed because all events that have not been
        // processed until this point would just get ignored when the
        // struct is dropped
        self.update_wait_time();
        thread::sleep(self.last_event.1.saturating_sub(Duration::from_millis(20)));
    }
}