How to block global keydown / keyup events in cocoa

I want to catch, change and cancel all keydown / keyup events in the system in my cocoa application. I know about CGEventTapCreate, but did not find any working code from the network.

thanks

+1
source share
1 answer

Found:

self.machPortRef = CGEventTapCreate(kCGSessionEventTap, kCGTailAppendEventTap, kCGEventTapOptionDefault, CGEventMaskBit(kCGEventKeyDown), (CGEventTapCallBack)eventTapFunction, self); if (self.machPortRef == NULL) { printf("CGEventTapCreate failed!\n"); } else { self.eventSrc = CFMachPortCreateRunLoopSource(NULL, self.machPortRef, 0); if ( self.eventSrc == NULL ) { printf( "No event run loop src?\n" ); }else { CFRunLoopRef runLoop = CFRunLoopGetCurrent(); //GetCFRunLoopFromEventLoop(GetMainEventLoop ()); // Get the CFRunLoop primitive for the Carbon Main Event Loop, and add the new event souce CFRunLoopAddSource(runLoop, self.eventSrc, kCFRunLoopDefaultMode); } } 

Properties:

 CFMachPortRef machPortRef; CFRunLoopSourceRef eventSrc; 

Event handler:

  CGEventRef eventTapFunction(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) { //printf("eventTap triggered\n"); return event; } 
+2
source

Source: https://habr.com/ru/post/987453/


All Articles