Pregunta

Estoy tratando de usar CGCREATEEVENTTTAP para monitorear los clics del mouse global, sin embargo, cuando hago esto, parece bloquear la interacción con mi propia aplicación.Los clics del mouse en otras aplicaciones en ejecución funcionan bien, pero mi propia aplicación (esa es la aplicación DemoAppDelegate) no responde completamente.Puedo arrastrar la ventana principal para la aplicación, pero los botones de la ventana roja / amarillo / verde están en gris.Y el menú de DemoApp es poco contracable también.

Esto me parece realmente extraño, y no he podido resolverlo.Ejemplos de uso de los grifos de eventos son pocos y distantes entre sí, por lo que cualquier consejo es muy apreciado.

#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

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top