The Awesome Guy The Awesome Guy - 1 year ago 142
Objective-C Question

Programmatically control a Mac cursor in Objective-C

There are a bunch of apps on the App Store that allow you to control your Mac's cursor - you install the app on your iPhone and some client software on your Mac, and then as you move your finger around on your phone and it controls your Mac through bluetooth.

My question is, how is this done? Not on the iOS side, but on the Mac side. How do you programmatically change the position of the cursor on a Mac?

Thanks in advance :)

Answer Source

As stated by @H2C03, you can do that via CGWarpMouseCursorPosition(), however you will only be able to warp the mouse location every 250 milliseconds (0.25 seconds). In order to get around that, you have to add a bit of extra code in order to continuously warp the mouse.

NSPoint mouseWarpLocation = NSMakePoint(100, 100);

CGEventSourceRef evsrc = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventSourceSetLocalEventsSuppressionInterval(evsrc, 0.0);
CGAssociateMouseAndMouseCursorPosition (0);
CGAssociateMouseAndMouseCursorPosition (1);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download