设置鼠标位置

时间:2011-11-09 02:21:26

标签: objective-c macos cocoa cursor

我需要能够将鼠标位置设置到屏幕/窗口的中间位置。 我怎么能这样做?

3 个答案:

答案 0 :(得分:9)

文档似乎表明CGDisplayMoveCursorToPointCGWarpMouseCursorPosition会做你想要的事。

编辑:为了符合您的最新评论,我会进一步推荐CGWarpMouseCursorPosition,其中包含以下文档:

  

例如,此功能通常用于移动光标位置   通过不想要光标的游戏回到屏幕中心   由显示边缘固定。

答案 1 :(得分:4)

我上周正在做类似的工作。

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

只需设置X和Y.

编辑:

#include <ApplicationServices/ApplicationServices.h>

答案 2 :(得分:-2)

您可以使用[NSEvent mouseLocation]来获取光标的当前位置,但我找不到任何直接将位置设置到屏幕中心的方法。

相关问题