当我按下空格键和箭头键时,我试图禁用错误声音。我尝试使用 处理事件super.keyDown(with: event) 没运气。除了使用全局关键框架之外,找不到任何其他可行的解决方案。我还有其他选择吗?
NSEvent.addLocalMonitorForEvents(matching: .keyDown) { (aEvent) -> NSEvent? in
self.keyDown(with: aEvent)
return aEvent
}
}
override func keyDown(with event: NSEvent) {
super.keyDown(with: event)
}
最佳答案
更新:我发现问题的根本原因是, View 是第一响应者,实际上不应该。将响应者设置为零后 self.view.window?.makeFirstResponder(nil)
我能够解决这个问题。我也用过 performKeyEquivalent
如 this answer suggested .
我知道我的回答很晚了,但也许将来会对您或其他人有所帮助。我不确定这是否是最好的方法,但它确实有效。只需返回 nil 而不是事件。
NSEvent.addLocalMonitorForEvents(matching: .keyDown) { (aEvent) -> NSEvent? in
self.keyDown(with: aEvent)
return nil
}
override func keyDown(with event: NSEvent) {
super.keyDown(with: event)
}
Apple 的方法文档对
block
做了以下说明范围:The event handler block object. It is passed the event to monitor. You can return the event unmodified, create and return a new NSEvent object, or return nil to stop the dispatching of the event.
唯一的缺点是,根本没有声音。即使关键事件不是由您处理的。
关于swift - OSX swift 3 - 无法禁用 keyDown。错误声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47887831/