开发者

How to end editing in NSTextFieldCell inside NSOutlineView after ESC key is pressed

开发者 https://www.devze.com 2023-03-02 09:57 出处:网络
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 NSControl

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

0

精彩评论

暂无评论...
验证码 换一张
取 消