以编程方式在OS X中模拟鼠标输入

问题描述:

是否可以模拟OS X中程序的鼠标操作?具体来说,简短的版本是我试图用两个网络摄像头来模拟触摸屏。因此,假设我可以获得X,Y位置,我可以将信息作为鼠标移动发送到操作系统,还是单击?以编程方式在OS X中模拟鼠标输入

编辑 - 或者如果在另一个操作系统中特别容易,我会愿意考虑这一点。

+0

根据您的使用情况下,有一个称为“UI录音机”的乐器可以记录您对节目的用户界面执行的任何操作,并在需要时进行播放。 – zneak 2010-04-29 00:55:08

+0

谢谢,但我真正想要做的是发送任何任意鼠标输入到操作系统。 – 2010-04-29 02:16:47

+3

我之前通过运行vnc服务器并编写了一个小型vnc客户端将鼠标事件发送到服务器。有开源的vnc服务器可以做到这一点,所以最好的办法是阅读一个源代码。 – 2010-04-29 02:34:41

是的,这是可能的。您可以使用Quartz Event Services来模拟输入事件。

假设C,我写了这个简单的例子:

#include <ApplicationServices/ApplicationServices.h> 
#include <unistd.h> 

int main() { 
    // Move to 200x200 
    CGEventRef move1 = CGEventCreateMouseEvent(
     NULL, kCGEventMouseMoved, 
     CGPointMake(200, 200), 
     kCGMouseButtonLeft // ignored 
    ); 
    // Move to 250x250 
    CGEventRef move2 = CGEventCreateMouseEvent(
     NULL, kCGEventMouseMoved, 
     CGPointMake(250, 250), 
     kCGMouseButtonLeft // ignored 
    ); 
    // Left button down at 250x250 
    CGEventRef click1_down = CGEventCreateMouseEvent(
     NULL, kCGEventLeftMouseDown, 
     CGPointMake(250, 250), 
     kCGMouseButtonLeft 
    ); 
    // Left button up at 250x250 
    CGEventRef click1_up = CGEventCreateMouseEvent(
     NULL, kCGEventLeftMouseUp, 
     CGPointMake(250, 250), 
     kCGMouseButtonLeft 
    ); 

    // Now, execute these events with an interval to make them noticeable 
    CGEventPost(kCGHIDEventTap, move1); 
    sleep(1); 
    CGEventPost(kCGHIDEventTap, move2); 
    sleep(1); 
    CGEventPost(kCGHIDEventTap, click1_down); 
    CGEventPost(kCGHIDEventTap, click1_up); 

    // Release the events 
    CFRelease(click1_up); 
    CFRelease(click1_down); 
    CFRelease(move2); 
    CFRelease(move1); 

    return 0; 
} 

并假设GCC,编译:

gcc -o program program.c -Wall -framework ApplicationServices

享受神奇。

+2

哇,对于一个老问题的很好的回答,谢谢! – 2010-10-03 23:25:25

+0

@Rob:不客气。我希望答案不会太晚。 – jweyrich 2010-10-04 00:32:49

+0

就像一个魅力 – piaChai 2014-07-02 08:14:55

如果你不想编译东西并且正在寻找一个基于shell的工具,Cliclick可能是解决方案。

这是基于jweyrick的回答工作的C程序:

// Compile instructions: 
// 
// gcc -o click click.c -Wall -framework ApplicationServices 

#include <ApplicationServices/ApplicationServices.h> 
#include <unistd.h> 

int main(int argc, char *argv[]) { 
    int x = 0, y = 0, n = 1; 
    float duration = 0.1; 

    if (argc < 3) { 
    printf("USAGE: click X Y [N] [DURATION]\n"); 
    exit(1); 
    } 

    x = atoi(argv[1]); 
    y = atoi(argv[2]); 

    if (argc >= 4) { 
    n = atoi(argv[3]); 
    } 

    if (argc >= 5) { 
    duration = atof(argv[4]); 
    } 

    CGEventRef click_down = CGEventCreateMouseEvent(
    NULL, kCGEventLeftMouseDown, 
    CGPointMake(x, y), 
    kCGMouseButtonLeft 
); 

    CGEventRef click_up = CGEventCreateMouseEvent(
    NULL, kCGEventLeftMouseUp, 
    CGPointMake(x, y), 
    kCGMouseButtonLeft 
); 

    // Now, execute these events with an interval to make them noticeable 
    for (int i = 0; i < n; i++) { 
    CGEventPost(kCGHIDEventTap, click_down); 
    sleep(duration); 
    CGEventPost(kCGHIDEventTap, click_up); 
    sleep(duration); 
    } 

    // Release the events 
    CFRelease(click_down); 
    CFRelease(click_up); 

    return 0; 
} 

托管在https://gist.github.com/Dorian/5ae010cd70f02adf2107

+2

我会使用'usleep(持续时间* 1000000)'。否则,你只会睡眠整整数秒! – nneonneo 2016-02-01 17:39:59

Swift鼠标移动,点击例如:

func mouseMoveAndClick(onPoint point: CGPoint) { 
    guard let moveEvent = CGEvent(mouseEventSource: nil, mouseType: .mouseMoved, mouseCursorPosition: point, mouseButton: .left) else { 
     return 
    } 
    guard let downEvent = CGEvent(mouseEventSource: nil, mouseType: .leftMouseDown, mouseCursorPosition: point, mouseButton: .left) else { 
     return 
    } 
    guard let upEvent = CGEvent(mouseEventSource: nil, mouseType: .leftMouseUp, mouseCursorPosition: point, mouseButton: .left) else { 
     return 
    } 
    moveEvent.post(tap: CGEventTapLocation.cghidEventTap) 
    downEvent.post(tap: CGEventTapLocation.cghidEventTap) 
    upEvent.post(tap: CGEventTapLocation.cghidEventTap) 
}