Detect hardware headphone presses in mac

◇◆丶佛笑我妖孽 提交于 2019-11-30 00:25:40
Daij-Djan

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!