From 4e62e8ad7abf833a3d2cb7ddfe01161e91c648ad Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 16 Aug 2018 06:32:51 +0200 Subject: HeadphoneKey: Detect 3 headphone button types Using my previous `NSLog`s, the `usageId`s came out to be 205, 233, 234. Converted these to hex and looked for corresponding constants in `/System/Library/Frameworks/IOKit.framework/Versions/A/Headers/hid/IOHIDUsageTables.h`. Use these constants to figure out which button was presed. Will have to test other headphones. I'm worried the numbers may change for different headphones. Added a `KeyPress` enum that's a lot easier to understand than the `upOrDown` `BOOL`, whose meaning I completely didn't get until I looked at the `NSLog` output I had before. --- DomeKey/HeadphoneKey.h | 5 +++++ DomeKey/HeadphoneKey.m | 17 +++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/DomeKey/HeadphoneKey.h b/DomeKey/HeadphoneKey.h index 16e74b2..0d4e729 100644 --- a/DomeKey/HeadphoneKey.h +++ b/DomeKey/HeadphoneKey.h @@ -9,6 +9,11 @@ #import #import +typedef enum KeyPress : BOOL { + KeyPressDown = YES, + KeyPressUp = NO +} KeyPress; + @interface HeadphoneKey : NSObject { NSArray *_mikeys; } diff --git a/DomeKey/HeadphoneKey.m b/DomeKey/HeadphoneKey.m index c01d2c5..0593173 100644 --- a/DomeKey/HeadphoneKey.m +++ b/DomeKey/HeadphoneKey.m @@ -28,10 +28,19 @@ press:(unsigned)usageId upOrDown:(BOOL)upOrDown { - NSLog(@"Clicked"); - NSLog(@"%d", usageId); - NSLog(@"%@", mikey); - NSLog(@"%d", upOrDown); + if (upOrDown == KeyPressUp) { + switch (usageId) { + case kHIDUsage_Csmr_PlayOrPause: + NSLog(@"Middle"); + break; + case kHIDUsage_Csmr_VolumeIncrement: + NSLog(@"Top"); + break; + case kHIDUsage_Csmr_VolumeDecrement: + NSLog(@"Bottom"); + break; + } + } } @end -- cgit v1.2.3