views:

127

answers:

2

Hi All,

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.

A: 

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.

drawnonward
Many thanks for the reply. Yes the CGEventTapCreate returned is null when i pass CGEventMaskBit(kCGEventKeyUp) as an argument but works fine with CGEventMaskBit(kCGEventFlagsChanged) as argument.If it's related to process running as root, then both the options should return NULL right? Please correct me If I'm wrong.What could be the issue?
Dheeraj
Only key up and key down events require special permission. Mouse and modifier key events are not protected.
drawnonward
A: 

I found the fix for this issue on this link :

http://www.cocoabuilder.com/archive/cocoa/242438-trouble-with-event-taps.html

Dheeraj