开发者

Issue with CGEventTapCreate() call

开发者 https://www.devze.com 2023-01-01 12:23 出处:网络
I\'m trying to register for global key events using this code : void function() { CFMachPortRef keyUpEventTap = CGEventTapCreate(kCGHIDEventTap,kCGHeadInsertEventTap,kCGEventTapOptionListenOnly,CGEv

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,kCGHeadInsertEventT开发者_StackOverflow社区ap,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 think you need special permission to register for keyboard events. I forget off hand what that is, but to test it run the program as root and see if it still crashes.

Edit:

According to this article you must either run the program as root or enable assistive devices.

The crash may just be because CGEventTapCreate returns NULL.

0

精彩评论

暂无评论...
验证码 换一张
取 消