When can they use ESC to close windows? 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 ?
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 &gt; 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.