Question

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 keyDown fired in the NSOutlineView.

Was it helpful?

Solution

Esc triggers -cancelOperation in NSResponder. You can try to handle this somewhere in your responder chain.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top