开发者

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

开发者 https://www.devze.com 2022-12-12 07:18 出处:网络
Here\'s my code: #import <ApplicationServices/ApplicationServices.h> CGEventRef myCGEventCallback(CGEventTapProxy proxy, CGEventType type,CGEventRef event, void *refcon) {

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.

0

精彩评论

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

关注公众号