Detect hardware headphone presses in mac

后端 未结 1 655
故里飘歌
故里飘歌 2020-12-24 09:26

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

相关标签:
1条回答
  • 2020-12-24 09:58

    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.

    0 讨论(0)
提交回复
热议问题