2
我使用DDHotKey來跟蹤某些系統範圍的鍵盤快捷鍵。當事件被觸發時,只有我的應用程序獲得它傳遞。是否有可能在不阻止將事件傳遞到其原始目標應用程序的情況下進行觀察?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;
}
僅當啓用輔助功能時,全局事件監視才起作用,這是我不希望用戶打擾的事情。 – 2013-02-28 20:38:38
這是沒有辦法的(除了以root身份運行)。有三種方法可以全局查看OS X上的關鍵事件:碳熱鍵,CGEventTap和NSEvent監視器(我懷疑它們是前者的包裝,後者都需要打開輔助功能) – 2013-02-28 21:00:03
啊,我我認爲有一些解決方法,謝謝反正 – 2013-02-28 21:05:27