你能用CGEventSetLocation来移动鼠标吗?

时间:2011-11-21 00:32:33

标签: core-graphics mouse

我有一个小程序,当它从屏幕左边超过500像素时,应将鼠标移动到某一点(在本例中为100,100)。 CGEventTap正确接收kCGEventMouseMoved事件,但CGEventSetLocation似乎只移动诸如mouseUp之类的事件,而不是MouseMoved。

是否可以使用CGEventSetLocation移动鼠标?如果没有,还有其他方法吗?

我在这里包含了我的代码:

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);
}

1 个答案:

答案 0 :(得分:0)

令人困惑的是,the function you want实际上不在Quartz Event Services中,而是Quartz Display Services