2010-04-29 67 views
52

是否可以模擬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

回答

82

是的,這是可能的。您可以使用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

7

如果你不想編譯東西並且正在尋找一個基於shell的工具,Cliclick可能是解決方案。

2

這是基於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

6

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) 
} 
相關問題