Version 19 of keysyms

Updated 2013-11-29 23:58:07 by pooryorick

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

See Also

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

Documentation

official reference

Description

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

Examples

bind . <KeyPress> {puts %K}

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

Corresponding Tkinter script:

from Tkinter import *

class Output(Label):
    def printkey(self, event):
        self.config(text=event.keysym)

root = Tk()
label = Label(root, text='Press a key...')
output = Output(root, takefocus=1)
label.pack()
output.pack()
output.focus()
output.bind('<KeyPress>', output.printkey)
root.mainloop()       

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!

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.

Peter Lewerin 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

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

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

_init_keypress_lookup
_keypress_lookup
_keypress
_keypress_string

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.

<<category>> X | Tk | Characters | Tk syntax help | Arts and crafts of Tcl-Tk programming