Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 329
Chase Walden Avatar asked Nov 09 '11 02:11

Chase Walden


2 Answers

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.

like image 58
Matt Wilding Avatar answered Oct 22 '22 08:10

Matt Wilding


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>
like image 34
sergiobuj Avatar answered Oct 22 '22 08:10

sergiobuj