@setfilename foo.info
@node bind
@subsection bind
@cartouche

bind \- Arrange for X events to invoke Tcl commands
@unnumberedsubsec Synopsis
@w{@b{bind}@i{ windowSpec}}
@w{@b{bind}@i{ windowSpec sequence}}
@w{@b{bind}@i{ windowSpec sequence command}}
@b{bind}@i{ windowSpec sequence @b{+}}\fIcommand
@end cartouche

@unnumberedsubsec Description

If all three arguments are specified, @b{bind} will
arrange for @i{command} (a Tcl
command) to be executed whenever the sequence of events given
by @i{sequence} occurs in the window(s) identified by @i{windowSpec}.
If @i{command} is prefixed with a ``+'', then it is appended to
any existing binding for @i{sequence};  otherwise @i{command} replaces
the existing binding, if any.  If @i{command}
is an empty string then the current binding for @i{sequence}
is destroyed, leaving @i{sequence} unbound.  In all of the cases
where a @i{command} argument is provided, @b{bind} returns
an empty string.

If @i{sequence} is specified without a @i{command}, then the
command currently bound to @i{sequence} is returned, or
an empty string if there is no binding for @i{sequence}.  If
neither @i{sequence} nor @i{command} is specified, then the
return value is a list whose elements are all the sequences
for which there exist bindings for @i{windowSpec}.

The @i{windowSpec} argument selects which window(s) the binding
applies to.
It may have one of three forms.
If @i{windowSpec} is the path name for a window, then the binding
applies to that particular window.
If @i{windowSpec} is the name of a class of widgets, then the
binding applies to all widgets in that class.
Lastly, @i{windowSpec} may have the value @b{all}, in which case
the binding applies to all windows in the application.

The @i{sequence} argument specifies a sequence of one or more
event patterns, with optional white space between the patterns.  Each
event pattern may
take either of two forms.  In the simplest case it is a single
printing ASCII character, such as @b{a} or @b{[}.  The character
may not be a space character or the character @b{<}.  This form of
pattern matches a @b{KeyPress} event for the particular
character.  The second form of pattern is longer but more general.
It has the following syntax:

@example
@b{<}@i{modifier-modifier-type-detail@b{>}}
@end example

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 (@i{detail}) identifying
a particular button or keysym.  Any of the fields may be omitted,
as long as at least one of @i{type} and @i{detail} is present.
The fields must be separated by white space or dashes.


Modifiers may consist of any of the values in the following list:

@example
   Control                         Any                                   
   Shift                           Double                                
   Lock                            Triple                                
   Button1, B1                     Mod1, M1, Meta, M                     
   Button2, B2                     Mod2, M2, Alt                         
   Button3, B3                     Mod3, M3                              
   Button4, B4                     Mod4, M4                              
   Button5, B5                     Mod5, M5                              
@end example

Where more than one value is listed, separated by commas, the values
are equivalent.   All of the modifiers except @b{Any},
@b{Double}, and @b{Triple} have
the obvious X meanings.  For example, @b{Button1} requires that
button 1 be depressed when the event occurs.  Under normal conditions
the button and modifier state at the time of the event must
match exactly those specified in the @b{bind} command.  If
no modifiers are specified, then events will match only if no modifiers
are present.  If the @b{Any} modifier is specified, then additional
modifiers may be present besides those specified explicitly.  For
example, if button 1 is pressed while the shift and control keys
are down, the specifier @b{<Any-Control-Button-1>} will match
the event, but the specifier @b{<Control-Button-1>} will not.


The @b{Double} and @b{Triple} modifiers are a convenience
for specifying double mouse clicks and other repeated
events. They cause a particular event pattern to be
repeated 2 or 3 times, and also place a time and space requirement
on the sequence:  for a sequence of events to match a @b{Double}
or @b{Triple} pattern, all of the events must occur close together
in time and without substantial mouse motion in between.
For example, @b{<Double-Button-1>}
is equivalent to @b{<Button-1><Button-1>} with the extra
time and space requirement.


The @i{type} field may be any of the standard X event types, with a
few extra abbreviations.  Below is a list of all the valid types;
where two name appear together, they are synonyms.

@example
   ButtonPress, Button        Expose               Leave                
   ButtonRelease              FocusIn              Map                  
   Circulate                  FocusOut             Property             
   CirculateRequest           Gravity              Reparent             
   Colormap                   Keymap               ResizeRequest        
   Configure                  KeyPress, Key        Unmap                
   ConfigureRequest           KeyRelease           Visibility           
   Destroy                    MapRequest           
   Enter                      Motion               
@end example



The last part of a long event specification is @i{detail}.  In the
case of a @b{ButtonPress} or @b{ButtonRelease} event, it is the
number of a button (1-5).  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.  If a button
number is given then @i{type} may be omitted:  if will default
to @b{ButtonPress}.  For example, the specifier @b{<1>}
is equivalent to @b{<ButtonPress-1>}.


If the event type is @b{KeyPress} or @b{KeyRelease}, then
@i{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 (``Shift_L'' is the keysm 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 should be
available in other X documentation.  If necessary, you can use the
@b{%K} notation described below to print out the keysym name for
an arbitrary key.  If a keysym @i{detail} is given, then the
@i{type} field may be omitted;  it will default to @b{KeyPress}.
For example, @b{<Control-comma>} is equivalent to
@b{<Control-KeyPress-comma>}.  If a keysym @i{detail} is specified
then the @b{Shift} modifier need not be specified and will be
ignored if specified:  each keysym already implies a particular
state for the shift key.


The @i{command} argument to @b{bind} is a Tcl command string,
which will be executed whenever the given event sequence occurs.
@i{Command} will be executed in the same interpreter that the
@b{bind} command was executed in.  If @i{command} contains
any @b{%} characters, then the command string will not be
executed directly.  Instead, a new command string will be
generated by replacing each @b{%}, and the character following
it, with information from the current event.  The replacement
depends on the character following the @b{%}, 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 of the substitutions are only valid for
certain types of events;  if they are used for other types of events
the value substituted is undefined.
@table @asis
@item @b{%%}
Replaced with a single percent.
@item @b{%#}
The number of the last client request processed by the server
(the @i{serial} field from the event).  Valid for all event
types.
@item @b{%a}
The @i{above} field from the event.
Valid only for @b{ConfigureNotify} events.
@item @b{%b}
The number of the button that was pressed or released.  Valid only
for @b{ButtonPress} and @b{ButtonRelease} events.
@item @b{%c}
The @i{count} field from the event.  Valid only for @b{Expose},
@b{GraphicsExpose}, and @b{MappingNotify} events.
@item @b{%d}
The @i{detail} field from the event.  The @b{%d} is replaced by
a string identifying the detail.  For @b{EnterNotify},
@b{LeaveNotify}, @b{FocusIn}, and @b{FocusOut} events,
the string will be one of the following:

@example
   NotifyAncestor                  NotifyNonlinearVirtual                
   NotifyDetailNone                NotifyPointer                         
   NotifyInferior                  NotifyPointerRoot                     
   NotifyNonlinear                 NotifyVirtual                         
@end example

For @b{ConfigureRequest} events, the substituted string will be
one of the following:

@example
   Above                              Opposite                           
   Below                              TopIf                              
   BottomIf                           
@end example

For events other than these, the substituted string is undefined.
@item @b{%f}
The @i{focus} field from the event (@b{0} or @b{1}).  Valid only
for @b{EnterNotify} and @b{LeaveNotify} events.
@item @b{%h}
The @i{height} field from the event.  Valid only for @b{Configure},
@b{ConfigureNotify}, @b{Expose}, @b{GraphicsExpose}, and
@b{ResizeRequest} events.
@item @b{%k}
The @i{keycode} field from the event.  Valid only for @b{KeyPress}
and @b{KeyRelease} events.
@item @b{%m}
The @i{mode} field from the event.  The substituted string is one of
@b{NotifyNormal}, @b{NotifyGrab}, @b{NotifyUngrab}, or
@b{NotifyWhileGrabbed}.  Valid only for @b{EnterWindow},
@b{FocusIn}, @b{FocusOut}, and @b{LeaveWindow} events.
@item @b{%o}
The @i{override_redirect} field from the event.  Valid only for
@b{CreateNotify}, @b{MapNotify}, @b{ReparentNotify},
and @b{ConfigureNotify} events.
@item @b{%p}
The @i{place} field from the event, substituted as one of the
strings @b{PlaceOnTop} or @b{PlaceOnBottom}.  Valid only
for @b{CirculateNotify} and @b{CirculateRequest} events.
@item @b{%s}
The @i{state} field from the event.  For @b{ButtonPress},
@b{ButtonRelease}, @b{EnterNotify}, @b{KeyPress}, @b{KeyRelease},
@b{LeaveNotify}, and @b{MotionNotify} events,
a decimal string
is substituted.  For @b{VisibilityNotify}, one of the strings
@b{VisibilityUnobscured}, @b{VisibilityPartiallyObscured},
and @b{VisibilityFullyObscured} is substituted.
@item @b{%t}
The @i{time} field from the event.  Valid only for events that
contain a @i{time} field.
@item @b{%v}
The @i{value_mask} field from the event.  Valid only for
@b{ConfigureRequest} events.
@item @b{%w}
The @i{width} field from the event.  Valid only for
@b{Configure}, @b{ConfigureRequest}, @b{Expose},
@b{GraphicsExpose}, and @b{ResizeRequest} events.
@item @b{%x}
The @i{x} field from the event.  Valid only for events containing
an @i{x} field.
@item @b{%y}
The @i{y} field from the event.  Valid only for events containing
a @i{y} field.
@item @b{%A}
Substitutes the ASCII character corresponding to the event, or
the empty string if the event doesn't correspond to an ASCII character
(e.g. the shift key was pressed).  @b{XLookupString} does all the
work of translating from the event to an ASCII character.
Valid only for @b{KeyPress} and @b{KeyRelease} events.
@item @b{%B}
The @i{border_width} field from the event.  Valid only for
@b{ConfigureNotify} and @b{CreateWindow} events.
@item @b{%D}
The @i{display} field from the event.  Valid for all event types.
@item @b{%E}
The @i{send_event} field from the event.  Valid for all event types.
@item @b{%K}
The keysym corresponding to the event, substituted as a textual
string.  Valid only for @b{KeyPress} and @b{KeyRelease} events.
@item @b{%N}
The keysym corresponding to the event, substituted as
a decimal
number.  Valid only for @b{KeyPress} and @b{KeyRelease} events.
@item @b{%R}
The @i{root} window identifier from the event.  Valid only for
events containing a @i{root} field.
@item @b{%S}
The @i{subwindow} window identifier from the event.  Valid only for
events containing a @i{subwindow} field.
@item @b{%T}
The @i{type} field from the event.  Valid for all event types.
@item @b{%W}
The path name of the window to which the event was reported (the
@i{window} field from the event).  Valid for all event types.
@item @b{%X}
The @i{x_root} field from the event.
If a virtual-root window manager is being used then the substituted
value is the corresponding x-coordinate in the virtual root.
Valid only for
@b{ButtonPress}, @b{ButtonRelease}, @b{KeyPress}, @b{KeyRelease},
and @b{MotionNotify} events.
@item @b{%Y}
The @i{y_root} field from the event.
If a virtual-root window manager is being used then the substituted
value is the corresponding y-coordinate in the virtual root.
Valid only for
@b{ButtonPress}, @b{ButtonRelease}, @b{KeyPress}, @b{KeyRelease},
and @b{MotionNotify} events.
@end table


If the replacement string
for a %-replacement contains characters that are interpreted
specially by the Tcl parser (such as backslashes or square
brackets or spaces) additional backslashes are added
during replacement so that the result after parsing is the original
replacement string.
For example, if @i{command} is

@example
@b{insert\0%A}
@end example

and the character typed is an open square bracket, then the command
actually executed will be

@example
@b{insert\0\e[}
@end example

This will cause the @b{insert} to receive the original replacement
string (open square bracket) as its first argument.
If the extra backslash hadn't been added, Tcl would not have been
able to parse the command correctly.


At most one binding will trigger for any given X event.
If several bindings match the recent events, the most specific binding
is chosen and its command will be executed.
The following tests are applied, in order, to determine which of
several matching sequences is more specific:
(a) a binding whose @i{windowSpec} names a particular window is
more specific than a binding for a class,
which is more specific than a binding whose @i{windowSpec} is
@b{all};
(b) a longer sequence (in terms of number
of events matched) is more specific than a shorter sequence;
(c) an event pattern that specifies a specific button or key is more specific
than one that doesn't;  (e) an event pattern that requires a particular
modifier is more specific than one that doesn't require the modifier;
(e) an event pattern specifying the @b{Any} modifier is less specific
than one that doesn't.  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.


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).


When a @i{sequence} specified in a @b{bind} command contains
more than one event pattern, then its command is executed whenever
the recent events (leading up to and including the current event)
match the given sequence.  This means, for example, that if button 1 is
clicked repeatedly the sequence @b{<Double-ButtonPress-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 @b{KeyPress} or @b{ButtonPress} events.
For example, @b{<Double-ButtonPress-1>} will match a sequence of
presses of button 1, even though there will be @b{ButtonRelease}
events (and possibly @b{MotionNotify} events) between the
@b{ButtonPress} events.
Furthermore, a @b{KeyPress} event may be preceded by any number
of other @b{KeyPress} events for modifier keys without the
modifier keys preventing a match.
For example, the event sequence @b{aB} will match a press of the
@b{a} key, a release of the @b{a} key, a press of the @b{Shift}
key, and a press of the @b{b} key:  the press of @b{Shift} is
ignored because it is a modifier key.
Finally, if several @b{MotionNotify} events occur in a row, only
the last one is used for purposes of matching binding sequences.


If an error occurs in executing the command for a binding then the
@b{tkerror} mechanism is used to report the error.
The command will be executed at global level (outside the context
of any Tcl procedure).

@unnumberedsubsec "See Also"
tkerror

@unnumberedsubsec Keywords
form, manual
