Apple's original HIGs (now disappeared from the web site, sadly) stated that:
The rightmost button in the dialog, the action button, is the button that confirms the alert message text. The action button is usually, but not always, the default button
In my case, I have some destructive operations (such as erasing a disk) that need "safe" confirmation dialogs, like this:
The worst option would be to make a dialog where the rightmost button would become the "do not erase" button, and the one left of it, which is usually, the Cancel button, would become the "erase" button, because that would lead easily to disaster (happened to me with a Microsoft-made dialog once), because people are trained to click the second button whenever they want to cancel an operation.
So, what I need is that the left (cancel) button becomes both the default button, and also reacts to the Return, Esc and cmd-period keys.
To make it default and also react to the Return key, I simply have to set the first button's keyEquivalent
to an empty string, and the second button's to "\r".
But how to I also make the alert cancel when Esc or cmd-. are typed?
Setup the NSAlert the way you normally would, with the default button assigned. Create a new subclass of NSView with an empty bounds and add it as the NSAlert's accessory view. In the subclass's
performKeyEquivalent
, check for Esc and if it matches call[-NSApplication stopModalWithCode:]
or[-NSWindow endSheet:returnCode:]
.