0027 – Menus II - Mnemonics (Shortcut Keys)
Today, we’ll cover two quick subjects, adding keyboard shortcuts to
MenuItems and separating menus into two areas. Let’s get at it.
Mnemonic Shortcut Keys
There are two ways to set up a keyboard shortcut for a
MenuItem. Both start with the call to
MenuItem’s constructor, but one passes three arguments while the other passes only two. In the following example, both are illustrated.
The Two-step Process
The code for creating the NewFileItem with its shortcut key uses the two-step process for hooking up the mnemonic and looks like this:
There are two things here that are different from our earlier examples:
- our constructor passes along an extra Boolean argument, and
newFileLabeltext has an underscore in front of the ‘N.’
The extra argument (
true) tells the super-class constructor to turn on the mnemonic for this
The underscore (
_) decides which key, combined with Alt, will activate the
And from there, it’s business as usual. Except that…
FileMenuHeader also has a mnemonic. Have a peek:
If you want a mnemonic on a
MenuItem, you need a mnemonic on the
Menu, too. It won’t show when your application is running (more’s the pity) and you don’t need to turn it on by passing a Boolean to the
MenuItem that acts as a menu header, but the underscore does have to be there.
The One-step Process
With this method, a pointer to the callback is passed to the super-class along with the mnemonic label text and the Boolean switch:
Notice also that the underscore isn’t under the first letter in the Label text, indicating that any one of the letters in the text
Label can be used as the shortcut key.
This is just about the easiest thing to do in GtkD. Pick the spot for the separator and:
SeparatorMenuItem separator = new SeparatorMenuItem(); append(separator);
No muss, fuss, or foaming at the mouth.
Well, that’s that. Mnemonic shortcut keys and separators… Yup.
If you'd like to leave a comment...
Until I get time to research and install a commenting system, I ask that you try one of these options:
- come on over to the D Language Forum and look for one of the gtkDcoding announcement posts,
- drop by the gtkD Forum,
- follow the link below to email me, or
- go to the gtkDcoding Facebook page.
You can also subscribe via RSS so you won't miss anything.
© Copyright 2019 Ron Tarrant