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

好久不见. 提交于 2019-12-12 08:53:09

问题


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??


回答1:


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.



来源:https://stackoverflow.com/questions/1776567/osx-quartz-event-taps-event-types-and-how-to-edit-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!