This question is concerning the code from this question: Detect headphone button presses in OS X
Their answer was marked correct, however I couldn't get their code to work (this may be because of my lack of knowledge of objective-c). I followed their instructions, and modified three files. This is what I have tried (I have also looked at Apple's IOKit documentation and could not find any headphone hardware button documentation):
AwesomeClass.h
#import <Cocoa/Cocoa.h>
#include "KeyboardPaneController.h"
@interface AwesomeClass : KeyboardPaneController
+ (NSArray *) allKeyboards;
- (void) initKeyboardElements: (NSArray *) elements;
- (void) ddhidQueueHasEvents: (DDHidQueue *) hidQueue;
@end
AwesomeClass.m
#import "AwesomeClass.h"
#import "DDHidLib.h"
@implementation AwesomeClass
+ (NSArray *) allKeyboards;
{
NSArray *array = [DDHidDevice allDevicesMatchingUsagePage: kHIDPage_Consumer
usageId: kHIDUsage_GD_Pointer
withClass: self
skipZeroLocations: NO];
//Only return "Apple Mikey HID Driver", if not found, return nil.
for (DDHidDevice *device in array) {
if ([[device productName] isEqualToString:@"Apple Mikey HID Driver"]) {
return [NSArray arrayWithObject:device];
}
}
return nil;
}
- (void) initKeyboardElements: (NSArray *) elements;
{
NSEnumerator * e = [elements objectEnumerator];
DDHidElement * element;
while (element = [e nextObject])
{
unsigned usagePage = [[element usage] usagePage];
unsigned usageId = [[element usage] usageId];
if (usagePage == kHIDPage_GenericDesktop)
{
if ((usageId >= 0x89) && (usageId <= 0x8D))
{
[mKeyElements addObject: element];
}
}
NSArray * subElements = [element elements];
if (subElements != nil)
[self initKeyboardElements: subElements];
}
}
- (void) ddhidQueueHasEvents: (DDHidQueue *) hidQueue;
{
DDHidEvent * event;
while ((event = [hidQueue nextEvent]))
{
DDHidElement * element = [self elementForCookie: [event elementCookie]];
unsigned usageId = [[element usage] usageId];
SInt32 value = [event value];
if (value == 1)
[self ddhidKeyboard: self keyDown: usageId];
}
}
@end
...as well as modifying one line in KeyboardPaneController.m (that was shown in the instructions). Due to the lack of documentation on the framework, it's really difficult to find an answer (the poster has been offline for more than a year, so I'm assuming I probably won't get a response back). The code was tested on lion, and I have mountain lion so that might be the issue.
I know that this is definitely possible to implement because iTunes is very responsive to my headphone-button-presses (which is a bit of a nuance). I'd like to be able to control Spotify with the buttons on my headphones, rather than iTunes.
A test shows it isn't implemented as a keyboard anymore:
id array = [[DDHidDevice allDevices] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"productName == \"Apple Mikey HID Driver\""]];
DDHidDevice *mic = [array count] ? [array objectAtIndex:0] : nil;
// it isn't a keyboard
NSLog(@"%@", mic.primaryUsage);
assert(mic.usage==1 && mic.usagePage==12);
You can't treat it like a HIDKeyboard either (tried, it doesn't send key presses), nor a HIDMouse or HID Joystick.
I dug into the HIDQueue class and checked if the mikey fires any low level events and it does fire low level events! The queueCallbackFunction
is called for presses (if you don't filter the queue for specific events. To filter it, see initWithKeyboardEvents
in DDHidKeyboard.
Now all you have to do is map the low level events to the correct buttons and you are done. Maybe wrap it all in a nice class too. Here's a sample of the class and relevant code on GitHub.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With