How can I dismiss dialog in Cocoa application when user presses Esc or Enter key? I have OK button, is it possible to make it default button?
+2
A:
If you present the alert panel using the NSAlert
class or, NSRunAlertPanel
family of functions, or the NSBeginAlertSheet
family of functions, you will get support for default and cancel buttons automatically.
If you're presenting a sheet that needs OK/Cancel buttons, and you're not using any of the above, you should be able to assign your buttons appropriate keyboard equivalents in Interface Builder using the attributes inspector. (Just highlight the Key Equiv. area and press the key you want to be equivalent to pressing that button.)
If you're presenting a dialog that's not either an alert or a document/window-modal sheet — don't. :) Document-modal alerts aren't Mac-like, and shouldn't be used for things like preferences windows.
Chris Hanson
2008-09-16 22:49:23
Fantastic, thank you very much!
Pavel Chuchuva
2008-09-16 23:06:59
There's a caveat: with NSRunAlertPanel/NSAlert I've found that the escape key only works if you call that button "Cancel". No idea how this works with localized apps.
Rhythmic Fistman
2008-11-13 22:44:30