我正在用 Swift 编写一个 OSX 应用程序,并且希望跟踪鼠标位置,即使该应用程序不是关键应用程序。此代码序列适用于我的 View ,但当应用程序不再是主应用程序时, View 停止接收 mouseMoved 事件:
window!.acceptsMouseMovedEvents = true
window!.makeMainWindow()
window!.makeKeyAndOrderFront(self)
window!.makeFirstResponder(self)
当我的应用程序不再是关键时,如何继续获取 mouseMoved 应用程序?
最佳答案
为了使其正常工作,需要启用辅助功能,或者必须信任您的应用程序的辅助功能。然后您可以调用该方法:
addGlobalMonitorForEventsMatchingMask(_:handler:)
这会为您想要接收通知的特定事件类型注册一个处理程序方法。
这是 Apple 关于 monitoring events 的文档.
关于macos - 操作系统: Track mouse position when not the key application,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28512068/