OSX:当不是关键应用程序时跟踪鼠标位置

时间:2015-02-14 03:21:41

标签: macos cocoa events swift

我正在Swift中编写OSX应用程序,并且即使应用程序不是关键应用程序也希望跟踪鼠标位置。此代码序列适用于我的View,但当应用程序不再是主应用程序时,视图停止接收mouseMoved事件:

    window!.acceptsMouseMovedEvents = true
    window!.makeMainWindow()
    window!.makeKeyAndOrderFront(self)
    window!.makeFirstResponder(self)

当我的应用程序不再是密钥时,如何继续获取mouseMoved应用程序?

1 个答案:

答案 0 :(得分:1)

为了使其工作,需要启用辅助功能,或者必须信任您的应用以获取辅助功能。然后,您可以调用方法:

addGlobalMonitorForEventsMatchingMask(_:handler:)

这将为您要接收通知的特定事件类型注册处理程序方法。

这是Apple关于monitoring events的文档。

相关问题