【发布时间】:2013-02-15 02:50:20
【问题描述】:
我正在使用DDHotKey 来跟踪一些系统范围的键盘快捷键。当事件被触发时,只有我的应用程序才能交付它。是否可以在不阻止事件传递到其原始目标应用程序的情况下对其进行观察?
这是该模块注册事件处理程序的方式:
InstallApplicationEventHandler(&dd_hotKeyHandler, 1, &eventSpec, NULL, NULL);
以及事件处理程序本身:
OSStatus dd_hotKeyHandler(EventHandlerCallRef nextHandler, EventRef theEvent, void *userData) {
@autoreleasepool {
EventHotKeyID hotKeyID;
GetEventParameter(theEvent, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(hotKeyID),NULL,&hotKeyID);
UInt32 keyID = hotKeyID.id;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"hotKeyID = %u", keyID];
NSSet *matchingHotKeys = [[DDHotKeyCenter sharedHotKeyCenter] hotKeysMatchingPredicate:predicate];
if ([matchingHotKeys count] > 1) { NSLog(@"ERROR!"); }
DDHotKey *matchingHotKey = [matchingHotKeys anyObject];
NSEvent *event = [NSEvent eventWithEventRef:theEvent];
NSEvent *keyEvent = [NSEvent keyEventWithType:NSKeyUp
location:[event locationInWindow]
modifierFlags:[event modifierFlags]
timestamp:[event timestamp]
windowNumber:-1
context:nil
characters:@""
charactersIgnoringModifiers:@""
isARepeat:NO
keyCode:[matchingHotKey keyCode]];
[matchingHotKey invokeWithEvent:keyEvent];
}
return noErr;
}
【问题讨论】:
标签: objective-c macos cocoa nsevent ddhotkey