Simulating mouse input programmatically in OS X

前端 未结 4 1278
悲&欢浪女
悲&欢浪女 2020-12-07 08:26

Is it possible to simulate the actions of a mouse from a program in OS X? Specifically, the short version is that I\'m trying to simulate a touchscreen using two webcams. So

4条回答
  •  囚心锁ツ
    2020-12-07 08:41

    Yes, it is possible. You can use the Quartz Event Services to simulate input events.

    Assuming C, I wrote this quick example:

    #include 
    #include 
    
    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;
    }
    

    And assuming GCC, compile with:

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

    Enjoy the magic.

提交回复
热议问题