Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Key-number bindings 1

Status
Not open for further replies.

Thakkaart

Programmer
Jun 14, 2002
2
NL
Is there any way to bind keys, such as Print Screen, Tab, Caps Lock, or any other key of a standard keyboard by the expression [ bind all <KEY> {objectname invoke etc.} ]?
It would be helpful if it is possible to just give the number of a key on the keyboard (like Escape=no.1, F1=no.2, etc.) between the < >.

Thomas
 
The Tk way is to give a symbolic name.
Here is the beginning of the Keysyms entry of the Tk manual:

Tk recognizes many keysyms when specifying key bindings (eg, bind . <Key-keysym>). The following list enumerates the keysyms that will be recognized by Tk. Note that not all keysyms will be valid on all platforms. For example, on Unix systems, the presence of a particular keysym is dependant on the configuration of the keyboard modifier map. This list shows keysyms along with their decimal and hexidecimal values.

space 32 0x0020
exclam 33 0x0021
quotedbl 34 0x0022
numbersign 35 0x0023
dollar 36 0x0024
percent 37 0x0025
ampersand 38 0x0026
quoteright 39 0x0027
parenleft 40 0x0028
parenright 41 0x0029
asterisk 42 0x002a
plus 43 0x002b
comma 44 0x002c
minus 45 0x002d
period 46 0x002e
slash 47 0x002f

...

You can find other useful info at
Good luck

ulis
 
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 &quot;Got an Escape key&quot;
}

but not with:

Code:
bind . <KeyPress-27> {
  puts &quot;Got an Escape key&quot;
}

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 &quot;%&quot; characters, and performs various substitutions before executing the script. In particular, &quot;%K&quot; is replaced by the keysym of a keyboard event, and &quot;%k&quot; 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 &quot;catch-all&quot; <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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top