I'm trying to get a custom NSTextFieldCell
(inside a NSOutlineView
) to end editing when the ESC key is pressed but cannot find any way to accomplish this. I tried to add an observer for the NSControlTextDidChangeNotification
-notification but it is not fired for the ESC-key nor is 开发者_如何学PythonkeyDown
fired in the NSOutlineView
.
Esc triggers -cancelOperation
in NSResponder. You can try to handle this somewhere in your responder chain.
The accepted answer is right. To elaborate: for catching ESC key events, you can override the cancelOperation method in the NSViewController (or any another derivative of NSResponder you're using). Here's what my code looks like in Swift 4.x.
class PopUIcontroller: NSViewController, NSTextFieldDelegate {
override func cancelOperation(_ sender: Any?) {
print("trying to cancel! Here I will do stuff to handle ESC key press!")
}
}
more reading: NSWindowController can't capture ESC without WebView is added to the window
精彩评论