Set the mouse location

安稳与你 提交于 2019-12-03 12:55:18

问题


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


回答1:


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.




回答2:


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>



回答3:


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.



来源:https://stackoverflow.com/questions/8059667/set-the-mouse-location

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