在 NSEventMask 中匹配多个 NSEvents

Matching multiple NSEvents in NSEventMask

以下语句处理在我的 macOS 应用程序中完成的任何左键单击。我希望捕获所有点击(左、右、中、滚轮等)。我知道我可以为它们中的每一个添加一个监视器,但是有没有办法将所有这些事件组合在一个事件掩码中?

NSEvent.addGlobalMonitorForEvents(matching: .leftMouseDown , handler: {
            (mouseEvent:NSEvent?) in .....

mask 参数的类型为 NSEventMask. NSEventMask conforms to OptionSet。因此,您可以使用 set 语法来表示多个选项:

NSEvent.addGlobalMonitorForEvents(matching: [.leftMouseDown, .rightMouseDown, .otherMouseDown] , handler: {
            (mouseEvent:NSEvent?) in .....