aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2018-10-13 17:58:24 +0200
committerTeddy Wing2018-10-13 17:58:24 +0200
commitd1954a7d49d010929f0444d4127dfe130a890a8e (patch)
tree1e277231a942a5493b4c3a108cc29a0993c0a5c9
parenta870c8c6f8a164bf2f46d513bb5ffab95f5c4d90 (diff)
downloadDomeKey-d1954a7d49d010929f0444d4127dfe130a890a8e.tar.bz2
Test code to press the "play" media key
Based on code from Albert https://stackoverflow.com/users/133374/albert and Nick Sweeting https://stackoverflow.com/users/2156113/nick-sweeting on Stack Overflow: - 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 Proof of concept code that simulates a key press of the "play" media key. Worked basically on the first try here, after the Rust version of the code giving me so much segfault hell. Going to adapt this to a generic function that I can pass as a pointer to the Rust code to call to send a media key event.
-rw-r--r--DomeKey/main.m25
1 files changed, 25 insertions, 0 deletions
diff --git a/DomeKey/main.m b/DomeKey/main.m
index f3c1eef..c77379b 100644
--- a/DomeKey/main.m
+++ b/DomeKey/main.m
@@ -26,6 +26,31 @@ int main(int argc, const char * argv[]) {
// insert code here...
NSLog(@"Hello, World!");
+ NSEvent *event1 = [NSEvent otherEventWithType:NSSystemDefined
+ location:NSZeroPoint
+ modifierFlags:0xa00
+ timestamp:0.0
+ windowNumber:0
+ context:nil
+ subtype:NSScreenChangedEventType
+ data1:(NX_KEYTYPE_PLAY << 16) | (0xa << 8)
+ data2:-1];
+ CGEventRef cg_event1 = [event1 CGEvent];
+ CGEventPost(kCGHIDEventTap, cg_event1);
+ CFRelease(cg_event1);
+ NSEvent *event2 = [NSEvent otherEventWithType:NSSystemDefined
+ location:NSZeroPoint
+ modifierFlags:0xb00
+ timestamp:0.0
+ windowNumber:0
+ context:nil
+ subtype:NSScreenChangedEventType
+ data1:(NX_KEYTYPE_PLAY << 16) | (0xb << 8)
+ data2:-1];
+ CGEventRef cg_event2 = [event2 CGEvent];
+ CGEventPost(kCGHIDEventTap, cg_event2);
+ CFRelease(cg_event2);
+
[Mappings observeReloadNotification];
[NSApp run];