From 8865b27d5436d6dcf6359c6f3eb9b3f9c17ad43a Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 7 Oct 2018 05:21:21 +0200 Subject: Try listening to media keys Use Peter Maurer's mechanism for getting media key events. Code taken from a Rogue Amoeba article. My hope was that this would allow us to intercept Bluetooth headphone key presses. It didn't. I'll likely be reverting this code. --- DomeKey/DKApplication.h | 2 ++ DomeKey/DKApplication.m | 38 ++++++++++++++++++++++++++++++++++++++ DomeKey/main.m | 3 ++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/DomeKey/DKApplication.h b/DomeKey/DKApplication.h index 6a92266..70ccd3a 100644 --- a/DomeKey/DKApplication.h +++ b/DomeKey/DKApplication.h @@ -10,4 +10,6 @@ @interface DKApplication : NSApplication +- (void)mediaKeyEvent:(int)key state:(BOOL)state repeat:(BOOL)repeat; + @end diff --git a/DomeKey/DKApplication.m b/DomeKey/DKApplication.m index 9449d06..4098ca7 100644 --- a/DomeKey/DKApplication.m +++ b/DomeKey/DKApplication.m @@ -10,4 +10,42 @@ @implementation DKApplication +// https://weblog.rogueamoeba.com/2007/09/29/ +- (void)sendEvent:(NSEvent *)theEvent +{ + if ([theEvent type] == NSSystemDefined && [theEvent subtype] == 8) { + int key_code = (([theEvent data1] & 0xFFFF0000) >> 16); + int key_flags = ([theEvent data1] & 0x0000FFFF); + int key_state = (((key_flags & 0xFF00) >> 8)) == 0xA; + int key_repeat = (key_flags & 0x1); + + [self mediaKeyEvent:key_code state:key_state repeat:key_repeat]; + } + + [super sendEvent:theEvent]; +} + +// https://weblog.rogueamoeba.com/2007/09/29/ +- (void)mediaKeyEvent:(int)key state:(BOOL)state repeat:(BOOL)repeat +{ + BOOL pressed_and_released = !state; + + if (pressed_and_released) { + switch (key) { + case NX_KEYTYPE_PLAY: + NSLog(@"Play"); + + break; + case NX_KEYTYPE_FAST: + NSLog(@"Fast"); + + break; + case NX_KEYTYPE_REWIND: + NSLog(@"Rewind"); + + break; + } + } +} + @end diff --git a/DomeKey/main.m b/DomeKey/main.m index aa2c66f..825e600 100644 --- a/DomeKey/main.m +++ b/DomeKey/main.m @@ -7,6 +7,7 @@ // #import +#import "DKApplication.h" #import "AppDelegate.h" #import "Mappings.h" #import "dome_key_map.h" @@ -18,7 +19,7 @@ int main(int argc, const char * argv[]) { return [Mappings dispatchReload]; } else if (config->args.daemon) { @autoreleasepool { - [NSApplication sharedApplication]; + [DKApplication sharedApplication]; AppDelegate *app = [[AppDelegate alloc] init]; [NSApp setDelegate:app]; -- cgit v1.2.3