| [ Team LiB ] |
|
Menu Bindings and EventsKeyboard TraversalThe 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 or a menu bar's cascade entry. The underline attribute 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 for a menubutton like this:
menubutton .menubar.file -text File -underline 0 \
-menu .menubar.file.m
If the File menu is implemented as a menu bar cascade, you create the traversal highlight like this:
menu .mbar
. configure -menu .mbar
.mbar add cascade -label File -underline 0 \
-menu .mbar.file
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. Menu Virtual EventsAs of Tk 8.0, a menu widget generates a <<MenuSelect>> virtual event whenever the menu's active entry changes. The event is fired after the menu selection has changed, so the binding action can access the new selection. The easiest way to be aware of changes to the menu selection is to bind to this virtual event, as shown in Example 30-8. Notification like this is useful for features such as context-sensitive help. Example 30-8 Using the <<MenuSelect>> virtual event
proc MenuChanged {w} {
puts "Menu $w selection: [$w entrycget active -label]"
}
bind .mbar.file <<MenuSelect>> {MenuChanged %W}
|
| [ Team LiB ] |
|