bind
Gives or sets the script that an event of a widget, a class, or another tag runs.
bind TAG
bind TAG SEQUENCE
bind TAG SEQUENCE SCRIPT
bind TAG SEQUENCE +SCRIPT
Description
A binding connects an event sequence of a tag to a script. TAG is the name of a widget, a class such as Button, all, or any other word. Each widget has a list of tags, which bindtags gives: by default the widget, its class, its toplevel, and all. An event of the widget runs the binding of each tag of the list, in order, that has a SEQUENCE that matches.
With TAG only, bind gives the sequences of TAG that have a binding. With SEQUENCE, it gives the script of that binding, or an empty string. With SCRIPT, it sets the binding and gives an empty string. A SCRIPT that starts with + adds its lines to the script of the binding. An empty SCRIPT deletes the binding.
A SEQUENCE is one or more events. Each event is <MODIFIER-MODIFIER-TYPE-DETAIL>, with any count of modifiers and an optional detail. A single character such as a is the key with that character. <<NAME>> is a virtual event of event.
- *Types*:
ButtonPress(orButton),ButtonRelease,Motion,Enter,Leave,KeyPress(orKey),KeyRelease,FocusIn,FocusOut,Configure,Destroy,Map,Unmap,MouseWheel,TouchpadScroll,Activate,Deactivate, andExpose. A scroll of a trackpad in pixels (a trackpad or a Magic Mouse on macOS, a precision touchpad of Windows, a touchpad on Wayland, or a wheel event of a browser in pixels) isTouchpadScroll, as in Tk 9; when no tag of the widget bindsTouchpadScroll, it isMouseWheelwith 120 for each 100 pixels, so a script for Tk 8.6 scrolls as before.Visibility,Create,Reparent,Gravity,Circulate,Property, andColormapare valid types too, and this page does not check when they occur. A detail with no type is a button number, such as<1>, or a key name, such as<a>. - *Modifiers*:
Control,Shift,Lock,Alt,Meta,Command,Option,Mod1toMod5(M1toM5),Button1toButton5(B1toB5),Double,Triple,Quadruple, andAny.Doublematches the second of two quick presses of the same button or key. - *Details*: a button number from 1 to 5 for a button event, and a key name such as
a,Return, orF9for a key event.
Before the script runs, each % field in it gets a value of the event.
- *`%W`*: the widget of the event.
- *`%x`, `%y`: the position of the pointer in the widget. `%X`, `%Y`*: the position on the screen.
- *`%b`: the button number. `%D`*: the distance of a
MouseWheelevent, and forTouchpadScrollthe pixels across and down in one number, each positive toward the start of the view:tk::PreciseScrollDeltas %Dgives them as a list. - *`%K`: the key name. `%A`: the text of the key. `%N`: the number of the key name. `%k`*: the key code.
- *`%s`: the state of the modifiers. `%t`: the time in milliseconds. `%T`*: the number of the event type.
- *`%w`, `%h`*: the width and the height of a
Configureevent. - *`%d`: the data of a virtual event. `%#`: the serial number. `%E`: 0. `%f`: the focus field of `Enter` and `Leave`, 0. `%%`*: one
%.
A field that the event does not have gives ??. The script runs at the global level. break in a script stops the bindings of the later tags for this event, and continue stops only the script of this tag. An error of the script goes to bgerror.
Returns
A list of sequences, a script, or an empty string.
Examples
% frame .f -width 20 -height 20
> pack .f
> update
> bind .f <Button-1> {set ::at %W:%x:%y:%b}
> event generate .f <Button-1> -x 3 -y 4
> set at
.f:3:4:1
# `<Key-a>` is shown in its short form `a`.
% bind .f <Key-a> {set ::key a}
> bind .f
a <Button-1>
# `<1>` and `<Button-1>` are the same sequence.
% bind .f <1>
set ::at %W:%x:%y:%b
% bind .f <Key-a> {+set ::again 1}
> bind .f <Key-a>
set ::key a
set ::again 1
% bind .f <Key-a> {}
> bind .f
<Button-1>
% frame .g -width 20 -height 20
> pack .g
> update
> set ::o {}
> bind .g <Button-1> {lappend ::o widget}
> bind Frame <Button-1> {lappend ::o class}
> bind all <Button-1> {lappend ::o all}
> event generate .g <Button-1>
> set o
widget class all
# `break` stops the bindings of the class and of `all`.
% set ::o {}
> bind .g <Button-1> {lappend ::o widget; break}
> event generate .g <Button-1>
> set o
widget
% bind .g <<Saved>> {set ::data %d}
> event generate .g <<Saved>> -data report.txt
> set data
report.txt
% bind .g <Configure> {set ::size %wx%h}
> .g configure -width 33
> update
> set size
33x20
Errors
% bind
wrong # args: should be "bind window ?pattern? ?command?"
-errorcode TCL WRONGARGS
% bind .f <Foo> x
bad event type or keysym "Foo"
-errorcode NONE
% bind .f <Button-9> x
bad button number "9"
-errorcode NONE
% bind .f <Key-nokey> x
bad event type or keysym "nokey"
-errorcode NONE
% bind .f <Key-a x
missing ">" in binding
-errorcode NONE
% bind .f <<A>><<B>> x
virtual events may not be composed
-errorcode NONE
Differences from Tcl
- The errors of
bindhave the-errorcodeNONE. Tk 8.6.16 gives a code such asTK EVENT KEYSYM. - A query of a SEQUENCE that is not valid gives an error in ztk. Tk 8.6.16 gives an empty string.
- A TAG that starts with a dot and is not a widget is a valid tag in ztk. Tk 8.6.16 gives the error
bad window path name. - The fields
%a,%B,%c,%i,%m,%o,%p,%P,%R,%S, and%vgive their letter in ztk, such ascfor%c. Tk 8.6.16 gives the value of the event or??. %kis 0 for a key ofevent generatein ztk. Tk 8.6.16 gives the key code of the X server.- The list of sequences shows
Mod1andMetaasAlt. Tk 8.6.16 keeps the name of the script. - An empty SEQUENCE gives the error
no event type or button # or keysym, and<<NAMEwith no>>givesvirtual events may not be composed. Tk 8.6.16 givesno events specified in bindingandmissing ">" in virtual binding.
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztk 1.0.0-beta.1