Menu Shortcuts

Menu shortcuts, also known as menu accelerators and command key equivalents, rarely accelerate anything. Nonetheless users often mistakenly think they do, and so a salable application needs them.

The java.awt.MenuShortcut class represents such a keyboard accelerator. This class has two constructors:

 public MenuShortcut(int key)
 public MenuShortcut(int key, boolean useShiftModifier)

In both cases the key argument is the raw keycode that will be used to invoke this menu item.

If useShiftModifier is true, then the shift key must be held down for this shortcut to be activated. useShiftModifier is false by default.

To add an accelerator key to a menu item, pass a MenuShortcut to the item's setShortcut() method. For example,

 MenuShortcut pShortcut = new MenuShortcut(KeyEvent.VK_P);
 MenuItem mi = new MenuItem("Print...");
 mi.setShortcut(pShortcut);

You can also just pass the shortcut to the MenuItem() constructor like this:

MenuShortcut pShortcut = new MenuShortcut(KeyEvent.VK_P);
MenuItem mi = new MenuItem("Print...", pShortcut);

Previous | Next | Top | Cafe au Lait

Copyright 1997, 1998, 2004 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified November 18, 2004