您在iPhone上使用的许多耳机(包括Apple自己的耳机)都有按键,麦克风或两者兼有。
它们可以很好地与Mac配合使用,并且iTunes可以识别按钮正确按下。
我的问题是 - 你如何在Cocoa中检测到这些按钮?我正在为您的菜单栏中的iTunes写一个小替代品,我还想回复耳机按钮,而不仅仅是键盘的媒体键。
感谢您的回复!
答案 0 :(得分:2)
在http://code.google.com/p/ddribin/查看DDHidLib。对于快速测试,您可以继承DDHidKeyboard并覆盖以下3种方法。然后,在提供的HIDDeviceTest目标的KeyboardPaneController.m - (void) awakeFromNib;
中,将NSArray * keyboards = [DDHidKeyboard allKeyboards];
替换为NSArray * keyboards = [<YourSubclass> allKeyboards];
或您命名为子类的任何内容。现在,当您运行HIDDeviceTest目标时,您应该会在“键盘”选项卡下看到“Apple Mikey HID驱动程序”。幸运的话,您将看到按下耳机遥控器按钮的输入。尝试双击并三击中间按钮,您将看到每个都是不同的事件类型。我只在2011年中期的13英寸Macbook空中运行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];
}
}