bind(n)
π NAME π
Okay bestie, welcome to bind(n) β this is one of the Tk Built-In Commands and it's genuinely iconic. What does it do? bind β arrange for X events to invoke Tcl scripts. That's the whole vibe β¨
π SYNOPSIS π
bind tag ?sequence? ?+??script?
πΈ INTRODUCTION πΈ
So bind is the command that links Tcl scripts to X events, no cap. Let's break down the three-argument case first, because that's the main character energy:
If you pass all three arguments, bind will arrange for script β a Tcl script, and yes it has a whole official name, it's called the binding script β to be evaluated whenever the event(s) given by sequence occur in the window(s) identified by tag. π«Ά
Now, the + situation. If script is prefixed with a +, then it gets appended to any existing binding for sequence. If there's no +? It replaces whatever binding was already there. Sorry to the old binding, it's over. π
And if script is an empty string, then the current binding for sequence is destroyed, leaving sequence unbound. Gone. Deleted. Not coming back.
In all of the cases where a script argument is provided, bind returns an empty string. That's it, that's the return value, don't @ me.
π Little lookup table for your brain: pass sequence with no script and you get the script currently bound to sequence back β or an empty string if there's no binding for sequence at all. Pass neither sequence nor script and the return value is a list whose elements are all the sequences for which there exist bindings for tag. Cute, right?
Which windows does this even apply to
The tag argument determines which window(s) the binding applies to. Here's the rule, and it's lowkey important:
- If tag begins with a dot, as in
.a.b.c, then it must be the path name for a window. - Otherwise it may be an arbitrary string. Yes, literally arbitrary. Slay.
Each window has an associated list of tags, and a binding applies to a particular window if its tag is among those specified for the window. The bindtags command may be used to assign an arbitrary set of binding tags to a window, but the default binding tags provide the following behavior:
- If a tag is the name of an internal window the binding applies to that window. π
- If the tag is the name of a class of widgets, such as
Button, the binding applies to all widgets in that class. - If the tag is the name of a toplevel window the binding applies to the toplevel window and all its internal windows.
- If tag has the value
all, the binding applies to all windows in the application. all is literally the main character of tags, iykyk.
π¦ EVENT PATTERNS π¦
The sequence argument specifies a sequence of one or more event patterns, with optional white space between the patterns. Each event pattern may take one of three forms, and bestie, we're going through all three.
Form 1: the baby one
In the simplest case it's a single printing ASCII character, such as a or [. Two rules though, and they matter:
The character may not be a space character or the character <. That's the whole constraint. No notes.
This form of pattern matches a Key event for the particular character. Simple, effective, eats every time. π
Form 2: the big general one
The second form of pattern is longer but more general. It has the following syntax:
<modifier-modifier-type-detail>
The entire event pattern is surrounded by angle brackets. Inside the angle brackets are zero or more modifiers, an event type, and an extra piece of information (detail) identifying a particular button or keysym.
Any of the fields may be omitted, as long as at least one of type and detail is present. That's the one rule you cannot break, fr fr. The fields must be separated by white space or dashes.
Form 3: the virtual event one
The third form of pattern is used to specify a user-defined, named virtual event. It has the following syntax:
<<name>>
The entire virtual event pattern is surrounded by double angle brackets. Inside the angle brackets is the user-defined name of the virtual event.
π Careful: modifiers, such asShiftorControl, may not be combined with a virtual event to modify it. It's not that kind of event.
The good news? Bindings on a virtual event may be created before the virtual event is defined, and if the definition of a virtual event changes dynamically, all windows bound to that virtual event will respond immediately to the new definition. Obsessed with the responsiveness honestly. β
Some widgets (e.g. menu and text) issue virtual events when their internal state is updated in some ways. Please see the manual page for each widget for details. π
β¨ MODIFIERS β¨
Modifiers consist of any of the following values:
ControlΒ·Mod1,M1,CommandAltΒ·Mod2,M2,OptionShiftΒ·Mod3,M3,NumLockΒ·Mod4,M4,FnExtendedΒ·Mod5,M5Button1,B1Β·Meta,MButton2,B2Β·DoubleButton3,B3Β·TripleButton4,B4Β·QuadrupleButton5,B5
Where more than one value is listed, separated by commas, the values are equivalent. Like, they're the same thing wearing different outfits π
Most of the modifiers have the obvious X meanings. For example, Button1 requires that button 1 be depressed when the event occurs. For a binding to match a given event, the modifiers in the event must include all of those specified in the event pattern. An event may also contain additional modifiers not specified in the binding β that's allowed, the binding isn't jealous.
π«Ά Example time, because this one's a classic: if button 1 is pressed while theshiftandcontrolkeys are down, the pattern<Control-Button-1>will match the event, but<Mod1-Button-1>will not.
And if no modifiers are specified, then any combination of modifiers may be present in the event. Anything goes. π
π Meta and Alt, the special ones
Meta and M refer to whichever of the M1 through M5 modifiers is associated with the Meta key(s) on the keyboard (keysyms Meta_R and Meta_L). If there are no Meta keys, or if they are not associated with any modifiers, then Meta and M will not match any events. Nothing. Zilch.
Similarly, the Alt modifier refers to whichever modifier is associated with the alt key(s) on the keyboard (keysyms Alt_L and Alt_R).
π₯ Double, Triple, Quadruple
The Double, Triple and Quadruple modifiers are a convenience for specifying double mouse clicks and other repeated events. They cause a particular event pattern to be repeated 2, 3 or 4 times, and also place a time and space requirement on the sequence: for a sequence of events to match a Double, Triple or Quadruple pattern, all of the events must occur close together in time and without substantial mouse motion in between. No drifting off mid-click, bestie.
For example, <Double-Button-1> is equivalent to <Button-1><Button-1> with the extra time and space requirement. Same energy, tighter rules. β¨
π The Mac-flavored ones
The Command, Option, Num and Fn modifiers are equivalents of Mod1 up to Mod4; they correspond to Macintosh-specific modifier keys.
π Extended
The Extended modifier is, at present, specific to Windows. It appears on events that are associated with the keys on the "extended keyboard". On a US keyboard, the extended keys include the Alt and Control keys at the right of the keyboard, the cursor keys in the cluster to the left of the numeric pad, the NumLock key, the Break key, the PrintScreen key, and the / and Enter keys in the numeric keypad. That's the whole extended crew, periodt.
π EVENT TYPES π
The type field may be any of the standard X event types, with a few extra abbreviations. The type field will also accept a couple non-standard X event types that were added to better support the Macintosh and Windows platforms. Below is a list of all the valid types; where two names appear together, they are synonyms:
ActivateButton,ButtonPressButtonReleaseCirculateCirculateRequestColormapConfigureConfigureRequestCreateDeactivateDestroyEnterExposeFocusInFocusOutGravityKey,KeyPressKeyReleaseLeaveMapMapRequestMotionMouseWheelPropertyReparentResizeRequestTouchpadScrollUnmapVisibility
Most of the above events have the same fields and behaviors as events in the X Windowing system. You can find more detailed descriptions of these events in any X window programming book. A couple of the events are extensions to the X event system to support features unique to the Macintosh and Windows platforms. We provide a little more detail on these events here. These include:
π Activate, Deactivate
These two events are sent to every sub-window of a toplevel when they change state. In addition to the focus Window, the Macintosh platform and Windows platforms have a notion of an active window (which often has but is not required to have the focus). On the Macintosh, widgets in the active window have a different appearance than widgets in deactive windows.
The Activate event is sent to all the sub-windows in a toplevel when it changes from being deactive to active. Likewise, the Deactive event is sent when the window's state changes from active to deactive.
π Heads up: there are no useful percent substitutions you would make when binding to these events. That's just how it is.
πΈ MouseWheel
Many contemporary mice include a mouse wheel, which is used for scrolling documents without using the scrollbars. By rolling the wheel, the system will generate MouseWheel events that the application can use to scroll. The event is routed to the window currently under the mouse pointer.
When the event is received you can use the %D substitution to get the delta field for the event, which is an integer value describing how the mouse wheel has moved. The smallest value for which the system will report is defined by the OS. The sign of the value determines which direction your widget should scroll:
- Positive values should scroll up β¬οΈ
- Negative values should scroll down β¬οΈ
Horizontal scrolling uses Shift-MouseWheel events, with positive %D delta substitution indicating left scrolling and negative right scrolling. Horizontal scrolling events are generated by tilt wheels on some mice. Horizontal scrolling can also be emulated by holding Shift and scrolling vertically. Big brain behavior, honestly.
π¦ TouchpadScroll
On some platforms (currently Windows and macOS) there is support for high-resolution scrolling devices, such as touchpads. This is provided via TouchpadScroll events. These events store two 16 bit delta values in the integer provided by the %D substitution. The X delta is in the high order 16 bits and the Y delta is in the low order 16 bits.
These values can be unpacked by using the tk::PreciseScrollDeltas utility procedure. For example:
lassign [tk::PreciseScrollDeltas %D] deltaX deltaY
The $# substitution is a counter for TouchpadScroll events which can be used by widgets that only support scrolling by units to ignore some portion of the events. π
π KeyPress, KeyRelease
The KeyPress and KeyRelease events are generated whenever a key is pressed or released. KeyPress and KeyRelease
π Key, KeyRelease
The Key and KeyRelease events are generated whenever a key is pressed or released. Key and KeyRelease events are sent to the window which currently has the keyboard focus. π
Button, ButtonRelease, Motion π±οΈβ¨
Okay so bestie, Button and ButtonRelease pop off when the user presses or releases a mouse button. Motion is the βthe pointer movedβ event β literally whenever the pointer is moved. These three are normally sent to the window containing the pointer. π«Ά
When a mouse button is pressed, the window containing the pointer automatically obtains a temporary pointer grab. That means subsequent Button, ButtonRelease, and Motion events get sent to that window β regardless of which window contains the pointer β until all buttons have been released. Not me being clingy, it's just the grab. π
Configure π
A Configure event is sent to a window whenever its size, position, or border width changes, and sometimes when it has changed position in the stacking order. It's giving βsomething about me changed, bestie.β β¨
Map, Unmap πΊοΈ
The Map and Unmap events are generated whenever the mapping state of a window changes. No cap.
Windows are created in the unmapped state. Top-level windows become mapped when they transition to the normal state, and are unmapped in the withdrawn and iconic states. Other windows become mapped when they are placed under control of a geometry manager (for example pack or grid). Slay for layout managers. π
A window is viewable only if it and all of its ancestors are mapped. The way that geometry managers typically do not map their children until they have been mapped themselves, and unmap all children when they become unmapped β hence in Tk Map and Unmap events indicate whether or not a window is viewable. Lowkey important. π
Visibility π
A window is said to be obscured when another window above it in the stacking order fully or partially overlaps it. Visibility events are generated whenever a window's obscurity state changes; the state field (%s) specifies the new state. Periodt. π
Expose π
An Expose event is generated whenever all or part of a window should be redrawn (for example, when a window is first mapped or if it becomes unobscured). It is normally not necessary for client applications to handle Expose events, since Tk handles them internally. Tk ate that one, fr fr. π«Ά
Destroy π
A Destroy event is delivered to a window when it is destroyed. RIP boo. π
β οΈ When the Destroy event is delivered to a widget, it is in a βhalf-deadβ state: the widget still exists, but operations that involve it may return invalid results, or return an error. Not me calling it half-dead, that's literally the term, bestie. π
FocusIn, FocusOut π―
The FocusIn and FocusOut events are generated whenever the keyboard focus changes. A FocusOut event is sent to the old focus window, and a FocusIn event is sent to the new one. It's giving focus handoff. β¨
In addition, if the old and new focus windows do not share a common parent, βvirtual crossingβ focus events are sent to the intermediate windows in the hierarchy. Thus a FocusIn event indicates that the target window or one of its descendants has acquired the focus, and a FocusOut event indicates that the focus has been changed to a window outside the target window's hierarchy. The keyboard focus may be changed explicitly by a call to focus, or implicitly by the window manager. Main character energy, honestly. π
Enter, Leave πͺ
An Enter event is sent to a window when the pointer enters that window, and a Leave event is sent when the pointer leaves it. Classic entrance/exit. π¦
If there is a pointer grab in effect, Enter and Leave events are only delivered to the window owning the grab. Also, when the pointer moves between two windows, Enter and Leave βvirtual crossingβ events are sent to intermediate windows in the hierarchy in the same manner as for FocusIn and FocusOut events. iykyk. π
Property π·οΈ
A Property event is sent to a window whenever an X property belonging to that window is changed or deleted. Property events are not normally delivered to Tk applications as they are handled by the Tk core. Tk is handling it, bestie, don't worry. π«Ά
Colormap π¨
A Colormap event is generated whenever the colormap associated with a window has been changed, installed, or uninstalled. Widgets may be assigned a private colormap by specifying a -colormap option; the window manager is responsible for installing and uninstalling colormaps as necessary. Note that Tk provides no useful details for this event type. It's giving mysterious. π
MapRequest, CirculateRequest, ResizeRequest, ConfigureRequest, Create π§
These events are not normally delivered to Tk applications. They are included for completeness, to make it possible to write X11 window managers in Tk. (These events are only delivered when a client has selected SubstructureRedirectMask on a window; the Tk core does not use this mask.) So if you're not writing an X11 window manager in Tk, you can lowkey chill. π
Gravity, Reparent, Circulate π
The events Gravity and Reparent are not normally delivered to Tk applications. They are included for completeness. π«Ά
A Circulate event indicates that the window has moved to the top or to the bottom of the stacking order as a result of an XCirculateSubwindows protocol request. Note that the stacking order may be changed for other reasons which do not generate a Circulate event, and that Tk does not use XCirculateSubwindows() internally. This event type is included only for completeness; there is no reliable way to track changes to a window's position in the stacking order. Not me trying to track stacking order with this and crying. π
EVENT DETAILS π
The last part of a long event specification is detail. In the case of a Button or ButtonRelease event, it is the number of a button (1-9). If a button number is given, then only an event on that particular button will match; if no button number is given, then an event on any button will match. π₯
β οΈ Note: giving a specific button number is different than specifying a button modifier; in the first case, it refers to a button being pressed or released, while in the second it refers to some other button that is already depressed when the matching event occurs. This is the chaos. π
If a button number is given then type may be omitted: it will default to Button. For example, the specifier <1> is equivalent to <Button-1>. Slay, shorthand. β¨
If the event type is Key or KeyRelease, then detail may be specified in the form of an X keysym. 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 (e.g. Shift_L is the keysym 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. iykyk. π§
If a keysym detail is given, then the type field may be omitted; it will default to Key. For example, <Control-comma> is equivalent to <Control-Key-comma>. π
π BINDING SCRIPTS AND SUBSTITUTIONS π
Okay so bestie, the script argument to bind is a Tcl script β the βbinding scriptβ β and it runs every time the given event sequence occurs. It executes in the same interpreter that the bind command was executed in, and it runs at global level (only global variables will be accessible). If script contains any % characters, then the script does not get executed directly. Instead, a new script is generated by replacing each %, and the character following it, with info from the current event. The replacement depends on the character following the %, as defined in the list below. Unless otherwise indicated, the replacement string is the decimal value of the given field from the current event. Some substitutions are only valid for certain types of events; if you use them for other event types, the value substituted is undefined. No cap.
So if you use a substitution on the wrong event type, the substituted value is undefined β that's the rules, not me. π
%%β replaced with a single percent.%#β the number of the last client request processed by the server (theserialfield from the event). Valid for all event types.%aβ theabovefield from the event, formatted as a hexadecimal number. Valid only forConfigureevents. Indicates the sibling window immediately below the receiving window in the stacking order, or0if the receiving window is at the bottom.%bβ the number of the button that was pressed or released. Valid only forButtonandButtonReleaseevents.%cβ thecountfield from the event. Valid only forExposeevents. Indicates that there arecountpendingExposeevents which have not yet been delivered to the window.%dβ thedetailoruser_datafield from the event. The%dis replaced by a string identifying the detail. ForEnter,Leave,FocusIn, andFocusOutevents, the string will be one of:NotifyAncestor,NotifyNonlinearVirtual,NotifyDetailNone,NotifyPointer,NotifyInferior,NotifyPointerRoot,NotifyNonlinear,NotifyVirtual.
For ConfigureRequest events, the string will be one of: Above, Opposite, Below, None, BottomIf, TopIf.
For virtual events, the string will be whatever value is stored in the user_data field when the event was created (typically with event generate), or the empty string if the field is NULL. Virtual events corresponding to key sequence presses (see event add for details) set the user_data to NULL. For events other than these, the substituted string is undefined.
%fβ thefocusfield from the event (0or1). Valid only forEnterandLeaveevents.1if the receiving window is the focus window or a descendant of the focus window,0otherwise.%hβ theheightfield from the event. Valid for theConfigure,ConfigureRequest,Create,ResizeRequest, andExposeevents. Indicates the new or requested height of the window.%iβ thewindowfield from the event, represented as a hexadecimal integer. Valid for all event types.%kβ thekeycodefield from the event. Valid only forKeyandKeyReleaseevents.%mβ themodefield from the event. The substituted string is one ofNotifyNormal,NotifyGrab,NotifyUngrab, orNotifyWhileGrabbed. Valid only forEnter,FocusIn,FocusOut, andLeaveevents.%oβ theoverride_redirectfield from the event. Valid only forMap,Reparent, andConfigureevents.%pβ theplacefield from the event, substituted as one of the stringsPlaceOnToporPlaceOnBottom. Valid only forCirculateandCirculateRequestevents.%sβ thestatefield from the event. ForButton,ButtonRelease,Enter,Key,KeyRelease,Leave, andMotionevents, a decimal string is substituted. ForVisibility, one of the stringsVisibilityUnobscured,VisibilityPartiallyObscured, andVisibilityFullyObscuredis substituted. ForPropertyevents, substituted with either the stringNewValue(indicating that the property has been created or modified) orDelete(indicating that the property has been removed).%tβ thetimefield from the event. This is the X server timestamp (typically the time since the last server reset) in milliseconds, when the event occurred. Valid for most events.%wβ thewidthfield from the event. Indicates the new or requested width of the window. Valid only forConfigure,ConfigureRequest,Create,ResizeRequest, andExposeevents.%x,%yβ thexandyfields from the event. ForButton,ButtonRelease,Motion,Key,KeyRelease, andMouseWheelevents,%xand%yindicate the position of the mouse pointer relative to the receiving window. For key events on the Macintosh these are the coordinates of the mouse at the moment when an X11KeyEventis sent to Tk, which could be slightly later than the time of the physical press or release. ForEnterandLeaveevents, the position where the mouse pointer crossed the window, relative to the receiving window. ForConfigureandCreaterequests, thexandycoordinates of the window relative to its parent window.%Aβ substitutes the UNICODE character corresponding to the event, or the empty string if the event does not correspond to a UNICODE character (e.g. the shift key was pressed). On X11,XmbLookupString(orXLookupStringwhen input method support is turned off) does all the work of translating from the event to a UNICODE character. On X11, valid only forKeyevent. On Windows and macOS/aqua, valid only forKeyandKeyReleaseevents.%Bβ theborder_widthfield from the event. Valid only forConfigure,ConfigureRequest, andCreateevents.%Dβ this reports the delta value of aMouseWheelevent. The delta value represents the rotation units the mouse wheel has been moved. The sign of the value represents the direction the mouse wheel was scrolled.%Eβ thesend_eventfield from the event. Valid for all event types.0indicates that this is a βnormalβ event,1indicates that it is a βsyntheticβ event generated bySendEvent.%Kβ the keysym corresponding to the event, substituted as a textual string. Valid only forKeyandKeyReleaseevents.%Mβ the number of script-based binding patterns matched so far for the event. Valid for all event types.%Nβ the keysym corresponding to the event, substituted as a decimal number. Valid only forKeyandKeyReleaseevents.%Pβ the name of the property being updated or deleted (which may be converted to an XAtom usingwinfo atom.) Valid only forPropertyevents.%Rβ the root window identifier from the event. Valid only for events containing arootfield.%Sβ the subwindow window identifier from the event, formatted as a hexadecimal number. Valid only for events containing asubwindowfield.%Tβ thetypefield from the event. Valid for all event types.%Wβ the path name of the window to which the event was reported (thewindowfield from the event). Valid for all event types.%X,%Yβ thex_rootandy_rootfields from the event. If a virtual-root window manager is being used then the substituted values are the corresponding x-coordinate and y-coordinate in the virtual root. Valid only forButton,ButtonRelease,Enter,Key,KeyRelease,LeaveandMotionevents. Same meaning as%xand%y, except relative to the (virtual) root window.
The replacement string for a %-replacement is formatted as a proper Tcl list element. This means that spaces or special characters such as $ and { may be preceded by backslashes. This guarantees that the string will be passed through the Tcl parser when the binding script is evaluated. Most replacements are numbers or well-defined strings such as Above; for these replacements no special formatting is ever necessary. The most common case where reformatting occurs is for the %A substitution. For example, if script is
insert %A
and the character typed is an open square bracket, then the script actually executed will be
insert \[
This will cause the insert to receive the original replacement string (open square bracket) as its first argument. If the extra backslash had not been added, Tcl would not have been able to parse the script correctly. π
π MULTIPLE MATCHES π
Okay so plot twist, bestie: multiple bindings can match the same X event at the same time. If those bindings live on different tags, then each of the bindings will be executed, in order. By default, a binding for the widget gets executed first, then a class binding, then a binding for its toplevel, then an all binding. And yes, the bindtags command may be used to change this order for a particular window or to associate additional binding tags with the window. Slay.
Inside a binding script, continue and break are your little traffic controllers. If continue is invoked within a binding script, then this binding script β including all other β+β appended scripts β is terminated, but Tk will continue processing binding scripts associated with other tags. If break is invoked within a binding script, then that script terminates and no other scripts will be invoked for the event. Periodt.
Within a script called from the binding script, return -code ok may be used to continue processing (including β+β appended scripts), or return -code break may be used to stop processing all other binding scripts. Cute, right?
Now if more than one binding matches a particular event and they have the same tag, then the most specific binding is chosen and its script is evaluated. The following tests are applied, in order, to determine which of several matching sequences is more specific:
- (a) An event pattern that specifies a specific button or key is more specific than one that does not.
- (b) A sequence with the most highest-ordered patterns (in term of highest repetition count) is more specific than a sequence with less highest-ordered patterns.
- (c) If the modifiers specified in one pattern are a subset of the modifiers in another pattern, then the pattern with more modifiers is more specific.
- (d) A virtual event whose physical pattern matches the sequence is less specific than the same physical pattern that is not associated with a virtual event.
- (e) Given a sequence that matches two or more virtual events, one of the virtual events will be chosen, but the order is undefined.
If the matching sequences contain more than one event, then tests (c)-(e) are applied in order from the most recent event to the least recent event in the sequences. If these tests fail to determine a winner, then the most recently registered sequence is the winner. No extra tie-break drama beyond that, iykyk.
If there are two (or more) virtual events that are both triggered by the same sequence, and both of those virtual events are bound to the same window tag, then only one of the virtual events will be triggered, and it will be picked at random:
event add <<Paste>> <Control-y>
event add <<Paste>> <Button-2>
event add <<Scroll>> <Button-2>
bind Entry <<Paste>> {puts Paste}
bind Entry <<Scroll>> {puts Scroll}
If the user types Control-y, the <<Paste>> binding will be invoked. But if the user presses button 2, then one of either the <<Paste>> or the <<Scroll>> bindings will be invoked β and exactly which one gets invoked is undefined. Chaos, but documented chaos. π
If an X event does not match any of the existing bindings, then the event is ignored. An unbound event is not considered to be an error. Good to know, because ignoring stuff is lowkey a vibe.
πΈ MULTI-EVENT SEQUENCES AND IGNORED EVENTS πΈ
When a sequence specified in a bind command contains more than one event pattern, then its script is executed whenever the recent events (leading up to and including the current event) match the given sequence. So itβs like a little event-history check, bestie.
This means, for example, that if button 1 is clicked repeatedly the sequence <Double-Button-1> will match each button press but the first. If extraneous events that would prevent a match occur in the middle of an event sequence, then the extraneous events are ignored unless they are Key or Button events. For example, <Double-Button-1> will match a sequence of presses of button 1, even though there will be ButtonRelease events (and possibly Motion events) between the Button events. The math is mathing.
Furthermore, a Key event may be preceded by any number of other Key events for modifier keys without the modifier keys preventing a match. For example, the event sequence aB will match a press of the a key, a release of the a key, a press of the Shift key, and a press of the b key: the press of Shift is ignored because it is a modifier key. Love that for us.
Finally, if several Motion events occur in a row, only the last one is used for purposes of matching binding sequences. Only the freshest motion counts. β¨
π ERRORS π
If an error occurs in executing the script for a binding, then the bgerror mechanism is used to report the error. The bgerror command will be executed at global level (outside the context of any Tcl procedure). So yeah, itβs not hiding inside some procedure bubble. Important.
π EXAMPLES π
Arrange for a string describing the motion of the mouse to be printed out when the mouse is double-clicked:
bind . <Double-1> {
puts "hi from (%x,%y)"
}
A little GUI that displays what the keysym name of the last key pressed is:
set keysym "Press any key"
pack [label .l -textvariable keysym -padx 2m -pady 1m]
bind . <Key> {
set keysym "You pressed %K"
}
These are adorable and also extremely useful. Eat. π
π¦ SEE ALSO π¦
β KEYWORDS β
bindingevent
Footer sign-off, because we are literally at the end: source Tk, version 8.0, page bind(n). The date slot is not in the footer text here, so Iβm not inventing one, bestie β no cap π. Sending you off with maximum binding energy. Byeee! πβ¨
π¦ where this page comes from
- alpine 3.23: tk9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tk9-doc 9.0.3-r2 Β· TCL
- fedora 43: tk 1:9.0.2-1.fc43 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- fedora 43: tk 1:9.0.2-1.fc43 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- fedora 44: tk 1:9.0.2-1.fc44 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- fedora 44: tk 1:9.0.2-1.fc44 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- nixos 26.05: tk 9.0.3
- Copyright (c) 1990 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- Copyright (c) 1998 Scriptics Corporation
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms