views:

173

answers:

1

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 assuming I can get X,Y positions, can I send information to the OS as a mouse movement or click?

Edit- Or if it's particularly easy in another operating system I'd be willing to consider that.

+2  A: 

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

Assuming C, I wrote this quick example:

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

And assuming GCC, compile with:

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

Enjoy the magic.

jweyrich
Whoa, great answer to an old question, thanks!
Rob Lourens
@Rob: you're welcome. I hope the answer didn't come too late.
jweyrich