Matt S. es cierto que si se puede usar la API para obtener NSEvent
la ubicación del mouse Sin embargo, no tiene que sondear para obtener coordenadas continuamente. Puede utilizar un CGEventTap
lugar:
- (void) startEventTap {
//eventTap is an ivar on this class of type CFMachPortRef
eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionListenOnly, kCGEventMaskForAllEvents, myCGEventCallback, NULL);
CGEventTapEnable(eventTap, true);
}
CGEventRef myCGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
if (type == kCGEventMouseMoved) {
NSLog(@"%@", NSStringFromPoint([NSEvent mouseLocation]));
}
return event;
}
esta manera, su función myCGEventCallback
disparará cada vez que se mueve el ratón (sin importar si su aplicación es en primer plano o no), sin tener que sondear en busca de la información. No se olvide de CGEventTapEnable(eventTap, false)
y CFRelease(eventTap)
cuando haya terminado.
posible duplicado de http://stackoverflow.com/questions/1117065/cocoa-getting-the-current-mouse-position-on- the-screen – outis