How do I associate a menu item with a keyboard shortcut

In wxPython you can set up keyboard navigation and shortcuts for menu items. Figure 10.3 displays a sample menu with keyboard decoration added. Notice that the menu names have an underlined character, and that next to the item labeled Accelerated is a Ctrl-A.

Studies have shown that keyboard shortcuts are not always the time saver that you think they are. However, they are standard interface elements, and your users will expect them to be there. Shortcuts are also helpful for users with accessibility issues. Listing 10.6 displays the code for adding keyboard shortcuts to menu items.

□ Accelerator Example [_ llnlfcj


Simple menu item

Accelerated Ctrl-A


Menu items with keyboard shortcuts

Figure 10.3

Menu items with keyboard shortcuts

Listing 10.6 Associating keyboard shortcuts to menu items import wx class MyFrame(wx.Frame):

"Accelerator Example")

simple = menu.Append(-1, "Simple &menu item") accel = menu.Append(-1, "&Accelerated\tCtrl-A")


Creating a mnemonic

Creating an accelerator self.Bind(wx.EVT_MENU, self.OnSimple, simple) self.Bind(wx.EVT_MENU, self.OnAccelerated, accel) self.Bind(wx.EVT_MENU, self.OnExit, exit)

menuBar = wx.MenuBar() menuBar.Append(menu, "&Menu") self.SetMenuBar(menuBar)

„ ^ m , r Using an accelerator table acceltbl = wx.AcceleratorTable( [ <—1 °


def OnSimple(self, event):

wx.MessageBox("You selected the simple menu item")

def OnAccelerated(self, event):

wx.MessageBox("You selected the accelerated menu item")

def OnExit(self, event): self.Close()

if __name__ == "__main__": app = wx.PySimpleApp() frame = MyFrame() frame.Show() app.MainLoop()

There are two kinds of keyboard shortcuts in wxPython; mnemonics and accelerators. Next, we'll be describing the differences between each type.

Using mnemonic shortcuts

A mnemonic is a single character that is used to access the menu item, and is represented visually by underlining that character. Mnemonics can be created by specifying the display text for the menu or menu item, and placing an ampersand before the character you want to use for the mnemonic, for example &File, &Edit, or Ma&cros. If you want an ampersand in your menu text, you must enter it as a double ampersand, for example, Font && Spacing.

Mnemonics are meant to be an alternate method for navigating a menu tree. They are only active when explicitly invoked by the user; under MS Windows this is done by pressing the alt key. Once the mnemonics are activated, the next keypress opens the top-level menu with the matching mnemonic. The key press after that opens the submenu or menu item within that menu, and so on until a menu item is selected, at which point a menu event is triggered as though the menu had been selected using the mouse. Mnemonic characters must be unique within the menu, but do not need to be unique across the entire menu bar. Typically, the first character of the menu display text is used as the mnemonic. If you have more than one menu item that starts with the same letter, there is no particular guideline governing which other character to use (the most common choices are the second letter or the last letter depending on which seems more reasonable). It's more important that the menu text be clear than to have the perfect mnemonic.

Using accelerator shortcuts

An accelerator in wxPython is a more typical keyboard shortcut, meaning a key combination that can be invoked any time which triggers the menu item. An accelerator can be created in two ways. The simpler way is to include the accelerator key combination in the display text of the menu or menu item when it is added to its parent. To do this, put a tab character \t after the text of your menu item. After the tab character, define the key combination. The first part of the key combination is one or more of Alt, Ctrl, or Shift, separated by either a + or a -, and followed by the actual key of the accelerator. For example: New\tctrl-n, Save As\tctrl-shift-s. Even if you only have one special key in the first part, you still use + or - to separate that part from the actual key. It does not matter if you use upper or lowercase for the key combination.

The actual key can be any number or letter or a function key, written as F1 through F12, plus one of the special words displayed in table 10.6 that represent other keys.

The wxPython methods ignore both mnemonics and accelerators for the purpose of finding a menu or menu item by its name. In other words, the call menubar.FindMenuItem("File", "Save Table 10.6 Non-alphanumeric accelerator keys

As"), will still match the Save As menu item, even if that item's display name was entered as Save &As\tctrl-shift-s.

Accelerators can also be created directly using an accelerator table, which is an instance of the class wx.AccleratorTable. An accelerator table consists of a list of wx.Accelra-torEntry objects. The constructor for wx.AcceleratorTable takes a list of accelerator entries, or it takes no arguments at all. In listing 10.6, we are taking advantage of the fact that wxPython will implicitly call the wx.Accel-eratorEntry constructor with the list of arguments, (wx.ACCEL_CTRL, ord('Q'), exit.GetId()). The constructor for wx.AcceleratorEntry is as follows.

wx.AcceleratorEntry(flags, keyCode, cmd)

The flags parameter is a bitmask with one or more of the following constants: wx.ACCEL_ALT, wx.ACCEL_CTRL, wxACCEL_ NORMAL, or wx.ACCEL_SHIFT. This parameter indicates which modifier keys need to be pressed to trigger the accelerator. The keyCode argument represents the regular key to be pressed to trigger the accelerator. It is either the ASCII number corresponding to a character, or one the special characters found in the wxWidgets documentation under Keycodes. The cmd argument is the wxPython identifier of the menu item which triggers its command event when the accelerator is invoked. As you can see from listing 10.6, declaring an accelerator in this way does not cause the key combination to be listed on the menu with the item display name. You still need to do that separately.








Down arrow
















Left arrow


Page down


Page Up




Right arrow


Space bar




Up arrow

□ Toggle Items Example

Toggle Items Check Hem 1

Check Hem 3

Radio Hem 1 Radio Hem 2 Radio Hem 3

Was this article helpful?

+1 0


  • joel
    What is the difference between keyboard shortcut and mnemonic?
    8 years ago
  • katie-leigh
    Do not show underline shortcut in wxpython menubar?
    7 years ago

Post a comment