diff options
-rw-r--r-- | DomeKey/AppDelegate.m | 1 | ||||
-rw-r--r-- | DomeKey/HeadphoneKey.h | 1 | ||||
-rw-r--r-- | DomeKey/HeadphoneKey.m | 39 |
3 files changed, 41 insertions, 0 deletions
diff --git a/DomeKey/AppDelegate.m b/DomeKey/AppDelegate.m index 3e4f3d8..318aeee 100644 --- a/DomeKey/AppDelegate.m +++ b/DomeKey/AppDelegate.m @@ -13,6 +13,7 @@ - (void)applicationDidFinishLaunching:(NSNotification *)notification { _headphone_key = [[HeadphoneKey alloc] init]; + [_headphone_key startMonitoringBluetoothEvents]; } @end diff --git a/DomeKey/HeadphoneKey.h b/DomeKey/HeadphoneKey.h index 861621f..586c40b 100644 --- a/DomeKey/HeadphoneKey.h +++ b/DomeKey/HeadphoneKey.h @@ -29,5 +29,6 @@ static const unsigned int TIMEOUT_MILLISECONDS = 1000; - (void)handleDeadKey:(HeadphoneButton)button; - (void)runAction; +- (void)startMonitoringBluetoothEvents; @end diff --git a/DomeKey/HeadphoneKey.m b/DomeKey/HeadphoneKey.m index ef90560..5a062fc 100644 --- a/DomeKey/HeadphoneKey.m +++ b/DomeKey/HeadphoneKey.m @@ -57,6 +57,45 @@ } } +- (void)startMonitoringBluetoothEvents +{ + // https://github.com/jguice/mac-bt-headset-fix/blob/master/Spotify%20Bluetooth%20Headset%20Listener/KDMAppDelegate.m + [NSEvent + addGlobalMonitorForEventsMatchingMask:(NSKeyDownMask | NSSystemDefinedMask) + handler:^(NSEvent *theEvent) { + int key_code = (([theEvent data1] & 0xFFFF0000) >> 16); + int key_flags = ([theEvent data1] & 0x0000FFFF); + int key_state = (((key_flags & 0xFF00) >> 8)) == 0xA; + + // TODO: Fix magic numbers + if (key_code == 10 && key_flags == 6972) { + switch ([theEvent data2]) { + case 786608: + case 786637: + NSLog(@"Play"); + + break; + case 786611: + NSLog(@"Next"); + + break; + case 786612: + NSLog(@"Previous"); + + break; + case 786613: + NSLog(@"Fast-forward"); + + break; + case 786614: + NSLog(@"Rewind"); + + break; + } + } + }]; +} + - (void)handleDeadKey:(HeadphoneButton)button { NSNumber *storable_button = [NSNumber numberWithUnsignedInteger:button]; |