CGEventTapCreate()调用的问题 [英] Issue with CGEventTapCreate() call
问题描述
我想使用以下代码注册全局关键事件:
void function()
{
CFMachPortRef keyUpEventTap = CGEventTapCreate(kCGHIDEventTap,kCGHeadInsertEventTap,kCGEventTapOptionListenOnly,CGEventMaskBit(kCGEventKeyUp),& keyUpCallback,NULL);
CFRunLoopSourceRef keyUpRunLoopSourceRef = CFMachPortCreateRunLoopSource(NULL,keyUpEventTap,0);
CFRelease(keyUpEventTap);
CFRunLoopAddSource(CFRunLoopGetCurrent(),keyUpRunLoopSourceRef,kCFRunLoopDefaultMode);
CFRelease(keyUpRunLoopSourceRef);
}
应用程序在执行CFMachPortCreateRunLoopSource()调用时崩溃。我认为崩溃是因为CGEventMaskBit( kCGEventKeyUp )当我创建一个事件点击。
但是如果我创建事件点击使用 CGEventTapCreate(kCGHIDEventTap,kCGHeadInsertEventTap,kCGEventTapOptionListenOnly,CGEventMaskBit(kCGEventFlagsChanged),& keyUpCallback,NULL)
,应用程序工作正常。它不会崩溃。我按任何修改键时收到回调。
谢谢,
Dheeraj。
我找到此链接上此问题的修复: / p>
http://www.cocoabuilder.com/archive/cocoa/242438-trouble-with-event-taps.html
I'm trying to register for global key events using this code :
void function()
{
CFMachPortRef keyUpEventTap = CGEventTapCreate(kCGHIDEventTap,kCGHeadInsertEventTap,kCGEventTapOptionListenOnly,CGEventMaskBit(kCGEventKeyUp),&keyUpCallback,NULL);
CFRunLoopSourceRef keyUpRunLoopSourceRef = CFMachPortCreateRunLoopSource(NULL, keyUpEventTap, 0);
CFRelease(keyUpEventTap);
CFRunLoopAddSource(CFRunLoopGetCurrent(), keyUpRunLoopSourceRef, kCFRunLoopDefaultMode);
CFRelease(keyUpRunLoopSourceRef);
}
The application crashes while executing CFMachPortCreateRunLoopSource() call. I think the crash is because of CGEventMaskBit(kCGEventKeyUp) when I create an event tap.
But if I create event tap using CGEventTapCreate(kCGHIDEventTap,kCGHeadInsertEventTap,kCGEventTapOptionListenOnly,CGEventMaskBit(kCGEventFlagsChanged),&keyUpCallback,NULL)
, the application works fine. It does not crash. I'm getting callbacks when any modifier key is pressed. But I need to get callbacks for delete key pressed.
Any ideas?
Thanks,
Dheeraj.
I found the fix for this issue on this link :
http://www.cocoabuilder.com/archive/cocoa/242438-trouble-with-event-taps.html
这篇关于CGEventTapCreate()调用的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!