Previous section   Next section

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 26.  Binding Commands to Events


A modifier indicates that another key or button is being held down at the time of the event. Typical modifiers are the Shift and Control keys. The mouse buttons can also be used as modifiers. If an event does not specify any modifiers, the presence of a modifier key is ignored by the event dispatcher. However, if there are two possible matching events, the more accurate match will be used. For example, consider these three bindings:

bind $w <KeyPress> {puts "key=%A"}
bind $w <Key-c> {puts "just a c"}
bind $w <Control-Key-c> {exit}

The last event is more specific than the others. Its binding will be triggered when the user types c with the Control key held down. If the user types c with the Meta key held down, the second binding will be triggered. The Meta key is ignored because it does not match any binding. If the user types something other than a c, the first binding is triggered. If the user presses the Shift key, then the keysym that is generated is C, not c, so the last two events do not match.

There are eight possible modifier keys. The Control, Shift, and Lock modifiers are found on nearly all keyboards. The Meta and Alt modifiers tend to vary from system to system, and they may not be defined at all. They are commonly mapped to be the same as Mod1 or Mod2, and Tk will try to determine how the mappings are set. The Macintosh has a Command modifier that corresponds to the clover-leaf or apple key. The remaining modifiers, Mod3 through Mod5, are sometimes mapped to other special keys. In OpenLook environments, for example, the Paste function key is also mapped to the Mod5 modifier.

The button modifiers, B1 through B5, are most commonly used with the Motion event to distinguish different mouse dragging operations. For example, <B1-Motion> is the event generated when the user drags the mouse with the first mouse button held down.


Double-click warning.

The Double and Triple events match on repetitions of an event within a short period of time. These are commonly used with mouse events. Be careful: The binding for the regular press event will match on the first press of the Double. Then the command bound to the Double event will match on the second press. Similarly, a Double event will match on the first two presses of a Triple event. Verify this by trying out the following bindings:

bind . <1> {puts stdout 1}
bind . <Double-1> {puts stdout 2}
bind . <Triple-1> {puts stdout 3}

If you click the first mouse button several times quickly, you will see a 1, 2, and then a few 3's output. Your bindings must take into consideration that more than one binding might match a Double or Triple event. This effect is compatible with an interface that selects an object with the first click, and then operates on the selected object with a Double event. In an editor, character, word, and line selection on a single, double, and triple click, respectively, is a good example.[*]

[*] If you really want to disable this, you can experiment with using after to postpone processing of one event. The time constant in the bind implementation of <Double> is 500 milliseconds. At the single-click event, schedule its action to occur after 600 milliseconds, and verify at that time that the <Double> event has not occurred.

Table 26-2 summarizes the modifiers.

Table 26-2. Event modifiers.
ControlThe control key.
ShiftThe shift key.
LockThe caps-lock key.
CommandThe command key. (Macintosh)
Meta, MDefined to be what ever modifier (M1 through M5) is mapped to the Meta_L and Meta_R keysyms.
AltDefined to be the modifier mapped to Alt_L and Alt_R.
Mod1, M1The first modifier.
Mod2, M2, AltThe second modifier.
Mod3, M3Another modifier.
Mod4, M4Another modifier.
Mod5, M5Another modifier.
Button1, B1The first mouse button (left).
Button2, B2The second mouse button (middle).
Button3, B3The third mouse button (right).
Button4, B4The fourth mouse button.
Button5, B5The fifth mouse button.
DoubleMatches double-press event.
TripleMatches triple-press event.
AnyMatches any combination of modifiers. (Before Tk 4.0)

The UNIX xmodmap program returns the current mappings from keys to these modifiers. The first column of its output lists the modifier. The rest of each line identifies the keysym(s) and low-level keycodes that are mapped to each modifier. The xmodmap program can also be used to change mappings. The following example shows the mappings on my system. Your setup may be different.

Example 26-2 Output from the UNIX xmodmap program.
xmodmap: up to 3 keys per modifier,
       (keycodes in parentheses):
shift Shift_L (0x6a), Shift_R (0x75)
lock Caps_Lock (0x7e)
control Control_L (0x53)
mod1 Meta_L (0x7f), Meta_R (0x81)
mod2 Mode_switch (0x14)
mod3 Num_Lock (0x69)
mod4 Alt_L (0x1a)
mod5 F13 (0x20), F18 (0x50), F20 (0x68)

      Previous section   Next section