Well, typically in situations like this, you don't really want the keycode. Instead, you want the
keysym. Tcl's event mechanisms use keysyms, not key codes. For those who might not know about these, here's a description of keysyms from the Tcl documentation for the
bind command:
"Keysyms are textual specifications for particular keys on the keyboard; they include all the alphanumeric ASCII characters (e.g. “a” is the keysym for the ASCII character “a”), plus descriptions for non-alphanumeric characters (“comma” is the keysym for the comma character), plus descriptions for all the non-ASCII keys on the keyboard (“Shift_L” is the keysm for the left shift key, and “F1” is the keysym for the F1 function key, if it exists). The complete list of keysyms is not presented here; it is available in other X documentation and may vary from system to system. If necessary, you can use the %K notation described below to print out the keysym name for a particular key. If a keysym detail is given, then the type field may be omitted; it will default to KeyPress. For example, <Control-comma> is equivalent to <Control-KeyPress-comma>."
Here's a quick little application that will let you interactively determine the keysym for any key on your system:
Code:
# -------------------------------------------
# FILE: events2.tcl
# DESCRIPTION: An example of creating
# widget bindings and using
# event substitutions.
# Display X/Y coordinates for
# <Motion> events, and keysyms
# and printable characters
# for <KeyPress> events.
# ==========================================
# Copyright (c) 2001 Ken Jones
# ==========================================
package require Tk
label .display -width 30 -height 3
pack .display -expand yes -fill both
bind .display <Motion> {
.display configure -text "Position = %x, %y"
}
bind .display <KeyPress> {
.display configure -text "Keysym = %K\nPrintable = %A"
}
focus .display
Playing around with this, you'll note that the keysym generated by pressing the "Enter" key is actually "Return", a throwback to the good ol' days when people still talked about carriage returns...
Anyway, this means that to create a binding on a widget that would respond to the Enter key being pressed while that widget has keyboard focus, you'd use a
bind command like this:
[tt]bind .t
<KeyPress-Return> {
# Whatever you want to do here...
}[/tt] - Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax