Question

J'essaie d'utiliser CGCreateEventTapTap pour surveiller les clics de souris globaux, cependant, lorsque je le fais, il semble bloquer l'interaction avec ma propre application.Les clics de souris dans d'autres applications de course fonctionnent bien, mais ma propre application (c'est-à-dire l'application Demoappdelegate) ne répond pas complètement.Je peux faire glisser la fenêtre principale de l'application, mais les boutons de fenêtre rouge / jaune / vert sont grisés.Et le menu de DemoApp est également neckable.

Cela me semble vraiment étrange et j'ai été incapable de le comprendre.Des exemples d'utilisation des robinets d'événement sont rares entre eux, donc tout conseil est grandement apprécié.

#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

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top