swift - 以编程方式控制 macOS 鼠标 - Swift 4
问题描述
我正在使用 Swift,我正在尝试弄清楚如何用我的 iPhone 控制我的 macOS 鼠标。我认为第一步是通过 macOS 应用程序以编程方式移动 MacOS 鼠标。我不确定我错过了什么或做错了什么。
import Cocoa
import CoreGraphics
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
typealias CGDirectDisplayID = UInt32
func CGMainDisplayID() -> CGDirectDisplayID{
return CGMainDisplayID()
}
func CGDisplayMoveCursorToPoint(_ display: CGDirectDisplayID,
_ point: CGPoint){
}
self.CGDisplayMoveCursorToPoint(CGMainDisplayID(),(25,400))
}
我收到一个错误:“预期声明”self.CGDisplayMoveCursorToPoint(CGMainDisplayID(),(25,400))
解决方案
我稍微改写了你的课。这应该完成你想要的:
typealias CGDirectDisplayID = UInt32
class ViewController: NSViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
moveCursor(onDisplay: mainDisplayID(), toPoint: CGPoint(x: 25, y: 400))
}
public func mainDisplayID() -> CGDirectDisplayID
{
return CGMainDisplayID()
}
public func moveCursor(onDisplay display: CGDirectDisplayID, toPoint point: CGPoint)
{
CGDisplayMoveCursorToPoint(display, point)
}
}
当前代码中最重要的错误是您的CGDisplayMoveCursorToPoint
函数在其定义中没有任何内容,因此不会发生任何事情。此外,你不想这样命名你的函数,因为它们是由 Core Graphics 框架实现的,编译器不会喜欢这样。Alexander 的评论似乎对您有所帮助。
在此自定义moveCursor
函数中,您可以调用其中一个CGDisplayMoveCursorToPoint(display, point)
或CGWarpMouseCursorPosition(point)
- 它们在功能上是等效的,但不需要显示 ID。从那里您需要moveCursor
在您的控制器生命周期方法之一中调用此方法。Alexander 的建议值得一提,但是对于开始并测试一切是否正常,您只需将其放入其中viewDidLoad
,该函数将在视图加载后立即启动。
希望这可以帮助!
推荐阅读
- java - listview/adapterview 仅在所有字段中显示 1 个值
- java - 使用自然 ID 进行休眠实体缓存
- string - 将工作日字符串解析为 time.Weekday
- javascript - jQuery .find() 返回未定义
- c - 数组接收的 MPI 总和仅适用于一级
- java - java.lang.AssertionError:预期状态:<201> 但原为:<400>
- jquery - Jquery Ajax Typo3 问题 - 加载主页
- javascript - 在 Node.js 中如何从 Php 中检索数据
- batch-file - 使用 start chrome 从 URL 下载文件
- javascript - 在边缘使用 location.reload 不起作用