CGEventTap blocks application input

I am trying to use CGCreateEventTap to monitor global mouse clicks, however, when I do this, it blocks interaction with my own application. Mouse clicks in other running applications work fine, but my own application (i.e. the DemoAppDelegate application) does not respond fully. I can drag the main application window, but the buttons of the red / yellow / green window are grayed out. And the DemoApp menu is also inconspicuous.

It seems really strange to me, and I could not understand it. Examples of using event taps are a bit far from each other, so any advice is welcome.

#import "DemoAppDelegate.h" CGEventRef myCGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) { CGPoint location = CGEventGetLocation(event); NSLog(@"location: (%f, %f) - %@\n", location.x, location.y, (NSString*)refcon); return event; } @implementation DemoAppDelegate @synthesize window; - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { CFMachPortRef eventTap; CGEventMask eventMask; CFRunLoopSourceRef runLoopSource; eventMask = 1 << kCGEventLeftMouseDown; eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, 1, eventMask, myCGEventCallback, @"mydata"); runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0); CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes); CGEventTapEnable(eventTap, true); CFRunLoopRun(); } @end 
+4
source share
1 answer

When you create a Cocoa application -[NSApplication run] is responsible for starting the event loop - it starts the execution loop and dispatches events. That means you have to delete this.

 CFRunLoopRun(); 

called at the bottom of the implementation of the -applicationDidFinishLaunching: method, since it prevents the -applicationDidFinishLaunching: method from returning, and also prevents the sending of NSApplication events.

+8
source

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


All Articles