Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 在监视器之间移动时,使用CGDisplayMoveCursor移动鼠标光标未按预期工作_Swift_Macos_Mouse - Fatal编程技术网

Swift 在监视器之间移动时,使用CGDisplayMoveCursor移动鼠标光标未按预期工作

Swift 在监视器之间移动时,使用CGDisplayMoveCursor移动鼠标光标未按预期工作,swift,macos,mouse,Swift,Macos,Mouse,所以我需要能够将鼠标移动到任意给定点(x和y)。我试着用CGDisplayMoveCursor来做这件事,当它移动光标时,它并没有把它放在我期望的地方。我有两个显示器在我的设置。我通过NSScreen.screens[I].frame获得帧,这些是它们的帧 Built-in Retina Display NSRect (0.0, 0.0, 1440.0, 900.0) SwitchResX4 - LG HDR WQHD NSRect (-1186.0, 900.0, 3840.0, 1600.

所以我需要能够将鼠标移动到任意给定点(x和y)。我试着用CGDisplayMoveCursor来做这件事,当它移动光标时,它并没有把它放在我期望的地方。我有两个显示器在我的设置。我通过NSScreen.screens[I].frame获得帧,这些是它们的帧

Built-in Retina Display
NSRect (0.0, 0.0, 1440.0, 900.0)

SwitchResX4 - LG HDR WQHD
NSRect (-1186.0, 900.0, 3840.0, 1600.0)
然后,当我收到新点时,我使用
NSRect.contains(NSPoint)
查找两个监视器中包含该点的监视器。那部分工作正常

但是,我想将光标移动到该点,但它不能像您预期的那样工作(光标移动,但不能像您基于帧所预期的那样移动到该点)。这是代码

let point = NSPoint(x: x, y: y)
guard let monitor = Monitor.displayIdWithPoint(point) else {
      SendlogCallbackEvent("D", "", "Found nil in monitor with point", #function, #file, #line)
 }      
 if CGDisplayMoveCursorToPoint(monitor.directDisplayId, point) == CGError.failure {
     SendlogCallbackEvent("D", "", "CGError.failer to move cursor", #function, #file, #line)
  }

我曾读到,这可能与CG和NS如何以不同的方式处理坐标系有关,但我还没有弄清楚如何使其工作。如何将NSPoint“转换”为CGPoint,使其与CGDisplayMove配合使用。。。功能。或者最好的方法是什么。

这段代码:

let point = NSPoint(x: x, y: y)
guard let monitor = Monitor.displayIdWithPoint(point) else {
      SendlogCallbackEvent("D", "", "Found nil in monitor with point", #function, #file, #line)
}
给我的印象是,您正在查找的
点是窗系统坐标空间(描述这两个窗框的坐标空间)中的一个点

但看一下的文档,我们看到(我的重点):

指向 本地显示空间中点的坐标。原点是指定显示的左上角

因此,我认为您需要获取
,并对其进行变换,使其相对于所需显示的原点:

let pointRelativeToScreen = point - monitor.frame.origin

if CGDisplayMoveCursorToPoint(monitor.directDisplayId, pointRelativeToScreen) == CGError.failure { ... }

或者,可能还有其他一些API,它需要一个以窗口系统坐标表示的光标点,它会自动为您将光标点弹出到正确的屏幕上。我现在没有访问Xcode的权限,所以我自己无法检查它。

是的,这就成功了。我认为我很好地阅读了文档,但显然我没有。非常感谢。我还尝试了CGEvent创建一个.mouseMove事件,但这似乎只在主屏幕上起作用?虽然我很沮丧,我本可以做得不对。