可可 – 在OS X中检测耳机按钮

前端之家收集整理的这篇文章主要介绍了可可 – 在OS X中检测耳机按钮前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
您在iPhone上使用的许多耳机(包括苹果自己的)都有按钮,麦克风或两者兼容.

他们可以很好的使用Mac,iTunes可以正确按下按钮.

我的问题是这样 – 你可以在Cocoa中如何检测这些按钮?我正在为您的菜单栏中的iTunes写一个小的替代品,我也想回应耳机按钮,而不仅仅是键盘的媒体按键.

感谢任何回应!

解决方法

检查DDHidLib为 http://code.google.com/p/ddribin/.对于快速测试,您可以继承DDHidKeyboard并覆盖以下3种方法.然后,在提供的HIDDeviceTest目标的KeyboardPaneController.m – (void)awakeFromNib中;替换NSArray * keyboards = [DDHidKeyboard allKeyboards];与NSArray * keyboards = [< YourSubclass> allKeyboards];或任何你命名为你的子类.现在当您运行HIDDeviceTest目标时,您应该看到“键盘”选项卡下列出的“Apple Mikey HID驱动程序”.幸运的是,您将看到按下耳机遥控器按钮的输入.尝试双击并三次敲击中间的按钮,您将看到每个都是不同的事件类型.我只在2011年中期的13“Macbook Air上运行Lion 10.7.3以及2010年中期的17”Macbook Pro运行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];
    }
}
原文链接:https://www.f2er.com/iOS/329360.html

猜你在找的iOS相关文章