How to assign shortcut keys to buttons

Hello,

I am building an app that will finally run in OS X.

In Windows I can use & symbol to assign a shotcut key to a button but when we run this app in OS X the shortcut keys just disappears and the use will always have to use mouse to click on a button.

So my question is how to assign shortcut keys buttons for apps targeted for OS X?

TIA

Yogi Yang

OSX does not have the concept of shortcut keys for buttons as does Win. Buttons in OSX do not get the focus so there is no way yo tell which button a key push was intended for. What I usually do is to duplicate the button’s function in a menu item. Then I can assign menu shortcuts.

You can also code it behind the scenes to trap the keystrokes, but Roger’s suggestion is the proper way to do it on a Mac.

The exception is if you use a MessageDialog. Although it won’t visually show you the shortcut key, you can use cmd- to press a button in a dialog if the button’s caption has the “&”.

OS X does have key equivalents, but they are rarely used and not supported by Xojo. You would have to use declares or plug-ins:

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Button/Tasks/SettingButtonKeyEquiv.html#//apple_ref/doc/uid/20000091-CJBCDJAD

[quote]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.[/quote]

While Apple lets you set them they don’t necessarily encourage their use in any of the HI guidelines :slight_smile:

Without menu equivalents they are hidden and not discoverable which isn’t a good Ui design (at least according to older HIG’s from MS and Apple that both seem to have lost this with the move to the “phone based view of the world”)

Still the wish for buttons reacting to more than Return and ESC arises from time to time …

So just in case (untested):

#If TargetMacOs Declare Sub setKeyEquivalent lib "AppKit" selector "setKeyEquivalent:" (handle as integer, key as cfstringRef) setkeyequivalent mybutton.handle, "A" #Endif

You could also make the button show the key, it seems:

[quote]If you set a key equivalent instead of an image, the button’s interior is redrawn. However, the key equivalent isn’t displayed if the image position is set to NSNoImage, NSImageOnly, or NSImageOverlaps; that is, the button must display both its title and its “image” (which is the key equivalent in this case), and they must not overlap.

To display a key equivalent on a button, set the image and alternate image to nil, set the key equivalent, and then set the image position.[/quote]

And then there’s the keyEquivalentModifier to make the button react only when a special key is pressed at the same time.

It is relatively easy to set up a timer that monitors Keyboard.AsyncKeyDown.

Ulrich : your declare works perfectly. Thank you.

[quote=249514:@Ulrich Bogun]Still the wish for buttons reacting to more than Return and ESC arises from time to time …
[/quote]
Sure
Things like save dialogs that respond to cmd-D as “Dont save” etc
But those are already handled

I’m sure there are others but, like I said, you should avoid hiding things from users in short cuts that are not discoverable etc