在OS X中检测耳机按钮按下 [英] Detect headphone button presses in OS X

查看:147
本文介绍了在OS X中检测耳机按钮按下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您在iPhone上使用的许多耳机(包括苹果自己的)都有按钮,麦克风或两者兼有。

Many headphones that you use on your iPhone (including Apple's own) have either buttons, a microphone or both.

它们与Mac很好地工作,按钮正确按下。

They work nicely with the Mac, and iTunes recognizes the button presses correctly.

我的问题是这 - 你如何在Cocoa中检测这些按钮?我写了一个小的替代iTunes的生活在你的菜单栏,我还想回应耳机按钮,而不只是键盘的媒体键。

My question is this - how would you detect these button presses in Cocoa? I'm writing a small alternative to iTunes that lives in your menu bar, and I'd want to also respond to the headset buttons, not just the keyboard's media keys.

感谢您的回应!

推荐答案

查看DDHidLib,网址为 http://code.google.com/p/ddribin/ 。对于快速测试,您可以子类化DDHidKeyboard并覆盖以下3个方法。然后,在提供的HIDDeviceTest目标的KeyboardPaneController.m - (void)awakeFromNib; ,替换 NSArray * keyboards = [DDHidKeyboard allKeyboards]; with NSArray * keyboards = [< YourSubclass> allKeyboards]; 或任何你命名的子类。现在,当您运行HIDDeviceTest目标时,您应该看到苹果Mikey HID驱动程序列在键盘选项卡下。运气,您将看到按下耳机遥控器按钮的输入。尝试双击和三击中间按钮,你会看到每个是不同的事件类型。我只在2011年中期测试这个13Macbook空气运行Lion 10.7.3以及2010年中期17Macbook Pro运行SL 10.6.8。

Check out DDHidLib at http://code.google.com/p/ddribin/. For a quick test, you can subclass DDHidKeyboard and override the 3 following methods. Then, in the provided HIDDeviceTest target's KeyboardPaneController.m - (void) awakeFromNib;, replace NSArray * keyboards = [DDHidKeyboard allKeyboards]; with NSArray * keyboards = [<YourSubclass> allKeyboards]; or whatever you named your subclass. Now when you run the HIDDeviceTest target, you should see "Apple Mikey HID Driver" listed under the "Keyboards" tab. With luck, you will see the input from pressing the headset remote buttons. Try double tapping and triple tapping the middle button and you will see that each one is a different event type. I've only tested this on a Mid 2011 13" Macbook air running Lion 10.7.3 as well as a Mid 2010 17" Macbook Pro running SL 10.6.8.

#import "DDHidLib.h"

@implementation <YourSubclass>

+ (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];
    }
}

这篇关于在OS X中检测耳机按钮按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆