Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OSX Quartz Event Taps: event types and how to edit events

Here's my code:

#import <ApplicationServices/ApplicationServices.h>

CGEventRef myCGEventCallback(CGEventTapProxy proxy, CGEventType type,  CGEventRef event, void *refcon) {
 printf("%u\n", (uint32_t)type);
 return event; 
}

int main (int argc, const char * argv[]) {
 CFMachPortRef eventTap;  
 CFRunLoopSourceRef runLoopSource; 

 eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, 0, kCGEventMaskForAllEvents, myCGEventCallback, NULL);
 runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
 CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
 CGEventTapEnable(eventTap, true);
 CFRunLoopRun();
    return 0;
}

First.. what if I wanted to edit the event? For example I listen for the keyDown event and if it's an "a" I turn it in a "b", or edit the mouse position in real time, or for example simply capture an event and make it have no effect (disabling a particular key for example..)

Second.. CGEventType is defined with an enum that lists only a few types.. for example when I hit CMD I get a 12, but that doesn't match the value specified in the enum.. what I'm I missing??

like image 564
luca Avatar asked Nov 21 '09 19:11

luca


Video Answer


1 Answers

To modify an event, there are various CGEventSet... functions. To kill the event, I think your tap function can just return NULL.

The enumeration for event types includes kCGEventFlagsChanged = NX_FLAGSCHANGED. If you look up IOKit/hidsystem/IOLLEvent.h, it defines NX_FLAGSCHANGED to be 12.

like image 167
JWWalker Avatar answered Oct 19 '22 05:10

JWWalker