diff options
| author | Teddy Wing | 2018-10-14 02:55:14 +0200 |
|---|---|---|
| committer | Teddy Wing | 2018-10-14 05:32:15 +0200 |
| commit | d5f7ca0e39fb965f392a0dfebd0bb7044b9dc5a6 (patch) | |
| tree | 4263f480bb73acbe5ddcdf73c72a8ab095d122a2 | |
| parent | 0f509e12d9ce3930ae600e0eacc2d683686272f4 (diff) | |
| download | dome_key_event_source_simulator-d5f7ca0e39fb965f392a0dfebd0bb7044b9dc5a6.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
Simulates a key press of the "play" media key. Going to adapt this to a
generic function.
| -rw-r--r-- | dome_key_event_source_simulator/dome_key_event_source_simulator.h | 5 | ||||
| -rw-r--r-- | dome_key_event_source_simulator/dome_key_event_source_simulator.m | 28 |
2 files changed, 32 insertions, 1 deletions
diff --git a/dome_key_event_source_simulator/dome_key_event_source_simulator.h b/dome_key_event_source_simulator/dome_key_event_source_simulator.h index 548e17b..9ccc35d 100644 --- a/dome_key_event_source_simulator/dome_key_event_source_simulator.h +++ b/dome_key_event_source_simulator/dome_key_event_source_simulator.h @@ -6,8 +6,11 @@ // Copyright © 2018 tw. All rights reserved. // -#import <Foundation/Foundation.h> +#import <Cocoa/Cocoa.h> @interface dome_key_event_source_simulator : NSObject @end + + +void dkess_press_key(int key, NSEventModifierFlags modifier_flags); diff --git a/dome_key_event_source_simulator/dome_key_event_source_simulator.m b/dome_key_event_source_simulator/dome_key_event_source_simulator.m index ddc8b5a..c1fa3cc 100644 --- a/dome_key_event_source_simulator/dome_key_event_source_simulator.m +++ b/dome_key_event_source_simulator/dome_key_event_source_simulator.m @@ -11,3 +11,31 @@ @implementation dome_key_event_source_simulator @end + +void dkess_press_key(int key, NSEventModifierFlags modifier_flags) { + 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); +} |
