keyDown
调用时机
示例代码
import Cocoa
class CustomView: NSView {
override func keyDown(with event: NSEvent) {
// 处理键盘按下事件
let keyPressed = event.charactersIgnoringModifiers ?? ""
print("Key pressed: \(keyPressed)")
// 根据按下的键执行相应操作
if keyPressed == "q" {
print("You pressed 'q'. Exiting...")
NSApplication.shared.terminate(nil)
}
}
override func acceptsFirstResponder() -> Bool {
return true // 确保视图可以接收键盘事件
}
}说明
注意事项
Last updated