I have a small program that should move the mouse to a certain point (in this case, 100,100) when it goes past 500 pixels from the left of the screen. The CGEventTap correctly receives the kCGEventMouseMoved events, but CGEventSetLocation seems to only move events such as mouseUp, not MouseMoved.
Is it possible to move the mouse with CGEventSetLocation? If not, is there some other way to do it?
I’ve included my code here:
CGEventRef
mouse_filter(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
if (type != kCGEventMouseMoved)
return event;
CGPoint point = CGEventGetLocation(event);
CGPoint target = CGPointMake(500,point.y);
if (point.x >= 500){
CGEventSetLocation(event,target);
printf("(%f,%f)\n", point.x, point.y);
}
return event;
}
int
main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
CFRunLoopSourceRef runLoopSource;
CGEventMask event_mask;
event_mask = (1 << kCGEventMouseMoved);
CFMachPortRef eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, 0, event_mask, mouse_filter, NULL);
if (!eventTap) {
NSLog(@"Couldn't create event tap!");
exit(1);
}
runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
CGEventTapEnable(eventTap, true);
CFRunLoopRun();
CFRelease(eventTap);
CFRelease(runLoopSource);
[pool release];
exit(0);
}
Confusingly, the function you want is actually not in Quartz Event Services, but Quartz Display Services.