1
我想在我的可可應用程序中捕獲,修改和轉移系統中的所有keydown/keyup事件。我知道CGEventTapCreate,但沒有發現任何工作代碼從網絡。如何在可可中捕獲全局keydown/keyup事件
感謝
我想在我的可可應用程序中捕獲,修改和轉移系統中的所有keydown/keyup事件。我知道CGEventTapCreate,但沒有發現任何工作代碼從網絡。如何在可可中捕獲全局keydown/keyup事件
感謝
找到解決方案:
self.machPortRef = CGEventTapCreate(kCGSessionEventTap,
kCGTailAppendEventTap,
kCGEventTapOptionDefault,
CGEventMaskBit(kCGEventKeyDown),
(CGEventTapCallBack)eventTapFunction,
self);
if (self.machPortRef == NULL)
{
printf("CGEventTapCreate failed!\n");
} else {
self.eventSrc = CFMachPortCreateRunLoopSource(NULL, self.machPortRef, 0);
if (self.eventSrc == NULL)
{
printf("No event run loop src?\n");
}else {
CFRunLoopRef runLoop = CFRunLoopGetCurrent(); //GetCFRunLoopFromEventLoop(GetMainEventLoop());
// Get the CFRunLoop primitive for the Carbon Main Event Loop, and add the new event souce
CFRunLoopAddSource(runLoop, self.eventSrc, kCFRunLoopDefaultMode);
}
}
屬性:
CFMachPortRef machPortRef;
CFRunLoopSourceRef eventSrc;
事件處理程序:
CGEventRef eventTapFunction(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon)
{
//printf("eventTap triggered\n");
return event;
}