Updated 2014-02-20 10:00:42 by PeterLewerin

keysyms are values that are recognized by Tk as names for certain keys. They are, for the most part, borrowed from X.

See Also  edit

KeySyms on platforms other than X11
keysyms for Tcl
Cross platform keysyms

Documentation  edit

official reference

Description  edit

A keysym can be used with [bind] to indicate a particular key.

Examples  edit

bind . <KeyPress> {puts %K}

Then press any key or key combination to learn the keysym for your desired key binding.

  Showing keysyms on a (modified) label widget in Python/Tkinter or Snit, or unmodified in Tk

Corresponding Tkinter script:
from Tkinter import *
class Output(Label):
def printkey(self, event):
root = Tk()
label = Label(root, text='Press a key...')
output = Output(root, takefocus=1)
output.bind('<KeyPress>', output.printkey)

Now you know why we use Tcl instead of Python!

fisheggs 2008-01-26:

To be fair, you're comparing an apple to an Orange Glazed Coffee Cake. So this is the Deep Dish Apple Pie version of the tcl example.
package require snit
::snit::widgetadaptor Output {
constructor {args} {
installhull using label
$self configurelist $args
method printkey {event} {
# I really should have created a ::snit::type that collected all
# the event values and made them available as instance variables
# and passed that in here, but to keep it simple....
$self config -text $event
delegate method * to hull
delegate option * to hull
label .label -text "Press a key..."
Output .output -takefocus 1
pack .label
pack .output
focus .output
bind .output <KeyPress> {.output printkey %K}

PYK 2013-11-28: Now you know why we don't use snit!

PL 2014-02-20: I certainly wouldn't say that. The point was that the Python example was able to modify a label widget (by subclassing it) so that it would change its text in response to keypress events, which went beyond the simple Tcl invocation that it was compared to. fisheggs then demonstrated how one could use Snit to modify a label in a similar way (by delegating to it). Modifying a widget will, by the nature of the matter, always be more verbose and complex than using it directly, but in both the Python and Snit examples you only have to deal with the complexity once, and as soon as the new class or snidget is in your library, their use is as straightforward as standard Tkinter or Tk code.

That said, I think I might have defined Output this way instead (if I had even bothered to support the -takefocus option: not doing so would have saved me one LOC):
::snit::widget Output {
hulltype ttk::label
delegate method printkey to hull as {config -text}
delegate option * to hull ;# if you really want to have -takefocus et al

which in turn demands that the widget type ttk::label be added to the list of hull types, if you haven't already done that:
lappend snit::hulltypes ttk::label

Use of this Output is the same as before.

But still, in this case adapting the label isn't really necessary in Tk (and I suspect not in Tkinter either): this is all the code that's actually needed:
label .label -text "Press a key..."
label .output -takefocus 1
pack .label
pack .output
focus .output
bind .output <KeyPress> {.output configure -text %K}

(and even then, you can save one line by combining the packs, but that's just being cheap).

CLN 2001-06-11: Is there any way to use accented characters (as used in many European languages) in bindings? I want, for example, Alt-e' (meant to be Alt pressed with an accented e) to be bound to a command.

PL 2001-06-30:
bind . <Alt-Key-eacute> ...

would seem to do what you want, but possibly only if you actually have a é key to press. At least for me, characters composed with dead keys don't fire the bindings, but character keys (e.g. adiaeresis on my keyboard) do.

Keysym vs Keycode  edit

Every key on a physical keyboard is assigned a number between 0 and 255. This is callled the keycode. The keycode is the unique identifier for the key, and no more than that, and has nothing really to do with human-language characters. A keysym, however, is very often either the character itself or something that a human would immediately recognize as indicating a certain character or key, e.g., "space".

Misc  edit

Mo Dejong included in the Tk test suite keypress-pertinent code. He advises, "See the following procs in tk/tests/event.test:

With these commands you can do the following:
_keypress_string $w HELLO\n

This will generate a keypress for each letter followed by an event for the return key.