swift - OSX swift 3 - 无法禁用 keyDown。错误声音

标签 swift macos

当我按下空格键和箭头键时,我试图禁用错误声音。我尝试使用 处理事件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)我能够解决这个问题。我也用过 performKeyEquivalentthis 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/

相关文章:

swift - navigationController?.navigationBar.isUserInteractionEnabled 没有按预期工作

apple/swift 中的 Swift 函数对象包装器

swift - 在移动平台上移动节点

macos - 如何增加osx上套接字的限制以进行负载测试?

objective-c - 将 XIB 链接到框架中的自定义 NSView

Swift:对于协议(protocol)在这种情况下有用的原因有点困惑

arrays - Swift:从数组中过滤特定范围的元素

xcode - Mac 上的 x86 汇编

java - 可以再次在 Catalina Mac OS 中从 root 写入或重定向

python - 使用 savefig() 将图形导出为 pdf 会弄乱 matplotlib 中的轴背景