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
source share