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);
To remove a shortcut, the MenuItem
's deleteShortcut()
method; for example:
mi.deleteShortcut();