I am developing a desktop application that supports one of the feature through Hot Key. I am using Event Tap for this to work.
But, sometimes (randomly) the callback is not invoked; Hot Key does not work and hence the feature seems to be not working.
Following is the code snippet:
-( void )startEventTapinThread //Called in a separate thread.
{
NSAutoreleasePool *pool =[ [ NSAutoreleasePool alloc] init];
CFRunLoopRef runloop =(CFRunLoopRef)CFRunLoopGetCurrent();
CGEventMask interestedEvents = CGEventMaskBit(kCGEventFlagsChanged)|CGEventMaskBit(kCGEventKeyDown);
CFMachPortRef eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, 0, interestedEvents, myCGEventCallback, self);
//self is the object pointer our method
CFRunLoopSourceRef source = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
CFRunLoopAddSource((CFRunLoopRef)runloop , source, kCFRunLoopCommonModes);
CFRunLoopRun();
[ pool release];
}
CGEventRef myCGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon)
{
CGEventType eventType = CGEventGetType(event);
//execute the code related to feature
}
After referring to mailing list queries I found that system disables event tap after some time. So, I am enabling the vent tap in my callback function:
CGEventType eventType = CGEventGetType(event);
if( eventType == kCGEventTapDisabledByTimeout)
{
CGEventTapEnable(my_eventtap_instance, YES);
return NULL;
}
But still the problem exists. After debugging for hours I noticed that myCGEventCallback is invoked only for kCGEventFlagsChanged ; It is not invoked only for kCGEventKeyDown. What am I doing wrong?
Could someone help me out.