CGEventTap blocks application input

浪子不回头ぞ 提交于 2019-12-22 08:24:48

问题


I'm trying to use CGCreateEventTap to monitor global mouse clicks, however when I do this it seems to block interaction with my own app. Mouse clicks in other running apps work fine, but my own app (that is the DemoAppDelegate app) does not respond completely. I can drag the main window for the app, but the red/yellow/green window buttons are greyed out. And the DemoApp's menu is unclickable as well.

This seems really strange to me, and I've been unable to figure it out. Examples of using event taps are few and far between, so any advice is greatly appreciated.

#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

回答1:


When you create a Cocoa application, -[NSApplication run] is responsible for running the event loop — it runs the run loop, and dispatches events. This means that you should remove that

CFRunLoopRun();

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



来源:https://stackoverflow.com/questions/5049217/cgeventtap-blocks-application-input

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