Previous section   Next section

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 27.  Buttons and Menus

Keyboard Traversal

The default bindings for menus allow for keyboard selection of menu entries. The selection process is started by pressing <Alt-x>, where x is the distinguishing letter for a menubutton. The underline attribute of a menubutton is used to highlight the appropriate letter. The underline value is a number that specifies a character position, and the count starts at zero. For example, a File menu with a highlighted F is created like this:

menubutton .menubar.file -text File -underline 0 \
    -menu .menubar.file.m

When the user types <Alt-f> over the main window, the menu is posted. The case of the highlighted letter is not important.

After a menu is posted, the arrow keys change the selected entry. The <Up> and <Down> keys move within a menu, and the <Left> and <Right> keys move between adjacent menus. The bindings assume that you create your menus from left to right.

If any of the menu entries have a letter highlighted with the -underline option, typing that letter invokes that menu entry. For example, an Export entry that is invoked by typing x can be created like this:

.menubar.file.m add command -label Export -underline 1 \
     -command File_Export

The <space> and <Return> keys invoke the menu entry that is currently selected. The <Escape> key aborts the menu selection and removes the menu.

      Previous section   Next section