Set the mouse location

╄→гoц情女王★ 提交于 2019-12-03 03:08:17

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.

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>

You can use [NSEvent mouseLocation] to get the curser's current location, but I couldn't find any ways of straight up setting the position to the center of the screen.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!