Button Programming Topics

Setting a Button’s Key Equivalent

A button can have a key equivalent, so that when the user presses that key, the button responds as though it’s been clicked.

Note that if you set the key equivalent to Return, that button becomes the default button.

You typically set a button’s key equivalent in Interface Builder. To do so, select the button and open the attributes pane of the inspector. Disclose the attributes for the button, click in the Key Equiv. field, and type the key or key combination you want to associate with the button. (You remove the key equivalent by pressing Clear.)

To set the key equivalent programmatically, use setKeyEquivalent: with the character. For example, to set it to Return, use:

[myButton setKeyEquivalent:@"\r"];

To set the button’s key equivalent to non-print character, you can use the key constants defined by NSResponder, as in the following example, which sets a button’s key equivalent to the left arrow key.