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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With