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
.
Asked
Active
Viewed 2,841 times
15

finnsson
- 4,037
- 2
- 35
- 44
2 Answers
27
Esc triggers -cancelOperation
in NSResponder. You can try to handle this somewhere in your responder chain.

Frederik Slijkerman
- 6,471
- 28
- 39
-
2Thanks. I ran with- (void)cancelOperation:(id)sender { [self abortEditing]; [[self window] makeFirstResponder:self]; } – finnsson Apr 30 '11 at 19:08
-
Thanks for this. I have been wondering how to do this kind of thing for a while. – Paul Shapiro Nov 27 '14 at 15:57
0
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

sdailey
- 2,030
- 2
- 15
- 13