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.