Note that Tcl allows you to create bindings using only the keysyms, not the keycodes. Thus, you could create a binding for the Escape key with:
Code:
bind . <KeyPress-Espace> {
puts "Got an Escape key"
}
but
not with:
Code:
bind . <KeyPress-27> {
puts "Got an Escape key"
}
If you want to create bindings for several different keys, you've got a couple of options. One is to create individual bindings for each individual key or key combination. This is often the best route to take, as you usually want to perform a different operation for each key received. The other advantage to this approach is that you can detect key combinations like Ctrl+q:
Code:
bind . <KeyPress-Espace> { # ...}
bind . <Control-KeyPress-q> { # ...}
bind . <KeyPress-A> { # ... }
The other option, which often is better if you want to respond to several different keys in a similar way, is to create a binding for all <KeyPress> events, and then use
event detail substitution to find out exactly which key was pressed. When Tcl runs an event binding, it first checks the action script for "%" characters, and performs various substitutions before executing the script. In particular, "%K" is replaced by the keysym of a keyboard event, and "%k" is replaced by the decimal keycode of an event. So you could do something like:
Code:
bind . <KeyPress> {
set keysym %K
set keycode %k
switch -exact -- $keycode {
32 { # Handle space key }
42 { # Handle asterisk key }
44 { # Handle comma key }
}
}
You can also combine these approaches. When an event comes in, Tcl tries to find the closest binding that it can to execute. So, if you've got the following bindings on a widget:
Code:
bind .w <KeyPress-F1> { # Handle Function 1 }
bind .w <KeyPress-F2> { # Handle Function 2 }
bind .w <KeyPress> { # Handle all other keys }
in this case, pressing F1 or F2 invokes their specific bindings, whereas pressing any other key invokes the "catch-all" <KeyPress> binding. - Ken Jones, President, ken@avia-training.com
Avia Training and Consulting,
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax