Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

So, I coincidentally noticed the following behavior:

Some windows can be closed by pressing the esc key, like the Preference window in any app or the 'Safari help' window or the 'Fonts' window in TextExit for example.

Other windows can not be closed by pressing the esc key, like the Safari browser window or the 'Special Characters' window.

The default cmd w always seems to work.

What is the rule for keystrokes on closing windows? When are we allowed to use esc ?

share|improve this question
    
IMHO it's probably a dev choice to bind esc to the closing action. –  Matthieu Riegler Jul 16 at 15:25
1  
@MatthieuRiegler Apple has some strict User Interface Guidelines. I hoped someone knows if this keybinding is noted in the guidelines somewere so I can predict this behaviour. –  CousinCocaine Jul 16 at 15:27
1  
Apps that don't obey one or the other, are terrible. Apps that obey neither, are worse. Literally rage-inducing. –  Jason Salaz Jul 23 at 3:37

2 Answers 2

up vote 7 down vote accepted

What you're seeing, namely, the behavior where the Escape key closes the window, is automatically built in to the NSPanel class, which is a subclass of NSWindow.

From Window Programming Guide > How Panels Work:

A panel is a special kind of window, typically serving an auxiliary function in an application. The NSPanel subclass of NSWindow adds a few special behaviors to windows in support of the role panels play:

...

If a panel is the key window and has a close button, it closes itself when the user presses the Escape key.

The preferences window (or panel) in many applications is implemented as an NSPanel: for example, Safari, Preview, Terminal, Console. The Fonts panel in Cocoa apps is implemented as an NSPanel. When you choose Safari > Safari Help, it actually launches an invisible background application, HelpViewer.app (/System/Library/CoreServices/HelpViewer.app), whose main window is implemented as a floating NSPanel. For these reasons, those panels will respond to the Escape key by closing the window. Standard general windows, which are instances of NSWindow, don't automatically get this behavior, as only panels are meant to behave that way.

share|improve this answer
    
thank you for this answer. It makes sense now. This also explains why cmd-period does not work in Safari's help window. 'Accepted' –  CousinCocaine Jul 17 at 7:37

Using the esc key to close a modal dialog is usually the same as pressing the Cancel button for the dialog. This is similar to how pressing the Return or Enter keys confirms the action for the button that is highlighted.

This has been in Apple's Human Interface Guidelines for Macintosh since the very beginning and is not new to OS X. The guideline can be found here under the "Dismissing Dialogs" heading:

https://developer.apple.com/library/mac/documentation/userexperience/conceptual/applehiguidelines/Windows/Windows.html#//apple_ref/doc/uid/20000961-TPXREF56

Search for "escape" on that page. The match is about 3/4 of the way down the page and is explicit about this behavior.

From the Human Interface Guidelines:

In general, include a Cancel button. The Cancel button returns the computer to the state it was in before the dialog appeared. It means “forget I mentioned it.” Also, make sure that the keyboard shortcut Command-period and the Esc (Escape) key are mapped to the Cancel button.

Another useful behavior, is if a dialog accepts Return or Enter as part of a text field, very often pressing Command-Return will press the default button.

share|improve this answer
    
This is the answer I am looking for, but I am still not completely satisfied yet ;) So <kbd>esc</kbd> is to close a model dialog and the guideline also states that command period should do the same. And command period does work in TextEdit's Font window, but it does not in Safari's Help window. Strange. –  CousinCocaine Jul 16 at 17:19
    
So if I interpreted the Font window as a dialog, than the esc or command period should close the window and return my program to the previous state. Therefor the help window (in the OP) is not a dialog. But why does esc work in the help window... From the guidelines: "The Cancel button returns the computer to the state it was in before the dialog appeared" –  CousinCocaine Jul 16 at 17:21
    
These are just guidelines. The developer can choose to ignore or extend them. You can find plenty of examples and counter examples in Apple's own apps. –  Mark Jul 16 at 17:56
2  
The examples the OP gave, namely most preferences windows in apps, the Fonts panel, Help Viewer.app's main window, aren't implemented as modal dialogs; rather, they are instances of NSPanel, which automatically receive this "Escape key causes panel to close" behavior. –  NSGod Jul 16 at 20:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.