我遇到了问题,这与此处描述的问题相反:cocoa-global-shortcuts。示例:当我分配“a”键作为快捷键(没有修饰符)时,每次按“a”我的回调被触发,但其他应用程序没有得到“a”按:TextEdit,XCode等我不想“消耗”这个事件。 这是代码:
EventTypeSpec eventTypePressed = { kEventClassKeyboard, kEventHotKeyPressed };
InstallApplicationEventHandler(&HotKeyPressedHandler, 1, &eventTypePressed, self, NULL);
EventTypeSpec eventTypeReleased = { kEventClassKeyboard, kEventHotKeyReleased };
InstallApplicationEventHandler(&HotKeyReleasedHandler, 1, &eventTypeReleased, self, NULL);
EventHotKeyID eventHotKeyID = { 'htky', 1 };
RegisterEventHotKey(shortcut.code, shortcut.flags, eventHotKeyID, GetApplicationEventTarget(), 0, &_eventHotKeyRef);
OSStatus HotKeyPressedHandler(EventHandlerCallRef nextHandler, EventRef theEvent, void *userData) {
ShortcutHandler* handler = userData;
[handler.delegate keyWasPressed:handler];
return CallNextEventHandler(nextHandler, theEvent);
}
OSStatus HotKeyReleasedHandler(EventHandlerCallRef nextHandler, EventRef theEvent, void *userData) {
ShortcutHandler* handler = userData;
[handler.delegate keyWasReleased:handler];
return CallNextEventHandler(nextHandler, theEvent);
}
谢谢!