swift - Send command shortcut to NSTextView's next responder
问题描述
I have an MacOS app that listens to key events by implementing NSWindow.keyDown(with event:)
. I use this to enable shortcuts like ⌘ n
to create a new item. This works well except when there is an NSTextView
that is the first responder. In that case, the text view will swallow the event and my keyDown
function will not get called. How do I prevent this and have the textview send events to the next responder (eventually my NSWindow
)?
解决方案
You can subclass NSTextView
override keyDown
method and call next responder:
import Cocoa
class TextView: NSTextView {
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
}
override func keyDown(with event: NSEvent) {
super.keyDown(with: event)
nextResponder?.keyDown(with: event)
}
}
推荐阅读
- python - 没有名为 (image_dehazer) 的模块
- json - postgresql:更新一个jsonb对象中的多个值
- python - 获取属性错误:模块“熊猫”没有属性“json_normalize”
- php - 带标题的强制文件下载会损坏文件
- javascript - html datalist 选项在空白处消失
- r - 使用 fviz_clust 显示所有标签
- javascript - 使用 Typescript 的 Nodejs 中的导入语句在编译后无法使用绝对路径解析
- android - 如何使用 Jetpack Compose 将片段添加到 Activity
- php - Eloquent::where() 条件未按预期工作,但 Collection::where() 确实有效
- openfeign - Feign客户端自定义请求对象有一个枚举字段,传递给服务时,这个枚举字段为空