1
votes

My application launches a modal sheet that has several buttons. One of them is the default button (key equivalent \r) and pressing the ENTER key on the keyboard dismisses the sheet, as expected. I would like to have the same effect to also be achieved if the user presses the ESC key. So either ENTER or ESC should be the key equivalent for the button. How can this be achieved?

1

1 Answers

4
votes

See -[NSResponder cancelOperation:], which is automatically bound to the escape key:

This method is bound to the Escape and Command-. (period) keys. The key window first searches the view hierarchy for a view whose key equivalent is Escape or Command-., whichever was entered. If none of these views handles the key equivalent, the window sends a default action message of cancelOperation: to the first responder and from there the message travels up the responder chain.

In other words, you can handle the escape key by implementing -cancelOperation: somewhere in your app's responder chain, such as the window controller.