aboutsummaryrefslogtreecommitdiffstats
path: root/src/key_code.rs
blob: e148555c4a1a41bd977cc4db146de2b9737598d0 (plain)
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
use cocoa::appkit::{
    NSEvent,
    NSEventModifierFlags,
    NSEventSubtype::NSScreenChangedEventType,
    NSEventType::NSSystemDefined,
};
use cocoa::base::nil;
use cocoa::foundation::NSPoint;
use core_graphics::event::{CGEvent, CGEventTapLocation, CGKeyCode, KeyCode};

// impl KeyCode {
//     pub const RETURN: CGKeyCode = 0x24;
//     pub const TAB: CGKeyCode = 0x30;
//     pub const SPACE: CGKeyCode = 0x31;
//     pub const DELETE: CGKeyCode = 0x33;
//     pub const ESCAPE: CGKeyCode = 0x35;
//     pub const COMMAND: CGKeyCode = 0x37;
//     pub const SHIFT: CGKeyCode = 0x38;
//     pub const CAPS_LOCK: CGKeyCode = 0x39;
//     pub const OPTION: CGKeyCode = 0x3A;
//     pub const CONTROL: CGKeyCode = 0x3B;
//     pub const RIGHT_COMMAND: CGKeyCode = 0x36;
//     pub const RIGHT_SHIFT: CGKeyCode = 0x3C;
//     pub const RIGHT_OPTION: CGKeyCode = 0x3D;
//     pub const RIGHT_CONTROL: CGKeyCode = 0x3E;
//     pub const FUNCTION: CGKeyCode = 0x3F;
//     pub const VOLUME_UP: CGKeyCode = 0x48;
//     pub const VOLUME_DOWN: CGKeyCode = 0x49;
//     pub const MUTE: CGKeyCode = 0x4A;
// }

//   kVK_F13                       = 0x69,
//   kVK_F14                       = 0x6B,
//   kVK_F15                       = 0x71,
//   kVK_F16                       = 0x6A,
//   kVK_F17                       = 0x40,
//   kVK_F18                       = 0x4F,
//   kVK_F19                       = 0x50,
//   kVK_F20                       = 0x5A,
//
// enum {
//   kVK_Return                    = 0x24,
//   kVK_Tab                       = 0x30,
//   kVK_Space                     = 0x31,
//   kVK_Delete                    = 0x33,
//   kVK_Escape                    = 0x35,
//   kVK_Command                   = 0x37,
//   kVK_Shift                     = 0x38,
//   kVK_CapsLock                  = 0x39,
//   kVK_Option                    = 0x3A,
//   kVK_Control                   = 0x3B,
//   kVK_RightCommand              = 0x36,
//   kVK_RightShift                = 0x3C,
//   kVK_RightOption               = 0x3D,
//   kVK_RightControl              = 0x3E,
//   kVK_Function                  = 0x3F,
//   kVK_F17                       = 0x40,
//   kVK_VolumeUp                  = 0x48,
//   kVK_VolumeDown                = 0x49,
//   kVK_Mute                      = 0x4A,
//   kVK_F18                       = 0x4F,
//   kVK_F19                       = 0x50,
//   kVK_F20                       = 0x5A,
//   kVK_F5                        = 0x60,
//   kVK_F6                        = 0x61,
//   kVK_F7                        = 0x62,
//   kVK_F3                        = 0x63,
//   kVK_F8                        = 0x64,
//   kVK_F9                        = 0x65,
//   kVK_F11                       = 0x67,
//   kVK_F13                       = 0x69,
//   kVK_F16                       = 0x6A,
//   kVK_F14                       = 0x6B,
//   kVK_F10                       = 0x6D,
//   kVK_F12                       = 0x6F,
//   kVK_F15                       = 0x71,
//   kVK_Help                      = 0x72,
//   kVK_Home                      = 0x73,
//   kVK_PageUp                    = 0x74,
//   kVK_ForwardDelete             = 0x75,
//   kVK_F4                        = 0x76,
//   kVK_End                       = 0x77,
//   kVK_F2                        = 0x78,
//   kVK_PageDown                  = 0x79,
//   kVK_F1                        = 0x7A,
//   kVK_LeftArrow                 = 0x7B,
//   kVK_RightArrow                = 0x7C,
//   kVK_DownArrow                 = 0x7D,
//   kVK_UpArrow                   = 0x7E
// };



    // unsafe fn otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2_(
    //     _: Self,
    //     eventType: NSEventType,
    //     location: NSPoint,
    //     modifierFlags: NSEventModifierFlags,
    //     timestamp: NSTimeInterval,
    //     windowNumber: NSInteger,
    //     context: id /* (NSGraphicsContext *) */,
    //     subtype: NSEventSubtype,
    //     data1: NSInteger,
    //     data2: NSInteger) -> id /* (NSEvent *) */


// https://stackoverflow.com/questions/11045814/emulate-media-key-press-on-mac/11048135#11048135
// https://stackoverflow.com/questions/10459085/cocoa-simulate-macbook-upper-keys-multimedia-keys/50574159#50574159
unsafe fn press_play() {
    let NX_KEYTYPE_PLAY = 16;
    let code = NX_KEYTYPE_PLAY;

    let key_down = NSEvent::otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2_(
        nil,
        NSSystemDefined,
        NSPoint::new(0.0, 0.0),
        NSEventModifierFlags::NSDeviceIndependentModifierFlagsMask,
        0.0,
        0,
        nil,
        NSScreenChangedEventType,
        (code << 16 as i32) | (0xa << 8 as i32),
        -1
    );
    let event = key_down.CGEvent() as *mut CGEvent;
    let event = &*event;
    event.post(CGEventTapLocation::HID);

    let key_up = NSEvent::otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2_(
        nil,
        NSSystemDefined,
        NSPoint::new(0.0, 0.0),
        NSEventModifierFlags::NSDeviceIndependentModifierFlagsMask,
        0.0,
        0,
        nil,
        NSScreenChangedEventType,
        (code << 16 as i32) | (0xb << 8 as i32),
        -1
    );
    let event = key_down.CGEvent() as *mut CGEvent;
    let event = &*event;
    event.post(CGEventTapLocation::HID);
}


#[cfg(test)]
mod tests {
    #[test]
    fn send_media_key_event() {
        press_play();
    }
}