Chase Walden
Chase Walden

Reputation: 1302

Set the mouse location

I need to be able to set the mouse location to the middle of the screen/window. How can I do that?

Upvotes: 8

Views: 6277

Answers (2)

sergiobuj
sergiobuj

Reputation: 2318

I was working on something like that last week.

  CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
  CGEventRef mouse = CGEventCreateMouseEvent (NULL, kCGEventMouseMoved, CGPointMake( X, Y), 0);
  CGEventPost(kCGHIDEventTap, mouse);
  CFRelease(mouse);
  CFRelease(source);

Just set X and Y.

EDIT:

#include <ApplicationServices/ApplicationServices.h>

Upvotes: 4

Matt Wilding
Matt Wilding

Reputation: 20163

The documentation seems to indicate that CGDisplayMoveCursorToPoint or CGWarpMouseCursorPosition will do what you're after.

EDIT: To match your latest comment, I would further recommend CGWarpMouseCursorPosition, about which the docs state:

For example, this function is often used to move the cursor position back to the center of the screen by games that do not want the cursor pinned by display edges.

Upvotes: 10

Related Questions