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 t开发者_JAVA技巧ime, 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??
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.
精彩评论