When you press Command-W
, it's the exact same as choosing File -> Close
from the menu bar. What Close
does is send a performClose:
message to the first responder. That, in turn, will check if the receiver or if the receiver's delegate implements windowShouldClose:
, and the window will close if it returns YES
(otherwise, it will call the close
method).
So really, it depends on what type of dialog you've got here. If it's non-modal (essentially, if you can access the menu bar while it's running) and is an instance or subclass of NSWindow
, then all you need to do is override the windowShouldClose:
method in your dialog's delegate (or your dialog class, if you subclassed NSWindow
or something) and make it return YES
.
However, if the dialog is a modal dialog (you can't access the menu bar, switch windows, etc. while the dialog is running), then you can't do it this way. You could add an invisible button, but in all honesty, a modal dialog should not be closed by hitting Command-W
, for that most certainly violates some Apple interface guideline out there. (Especially since, as Ande noted, it's standard practice to have Esc
close/cancel a dialog.)