1bind(n)                      Tk Built-In Commands                      bind(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       bind - Arrange for X events to invoke Tcl scripts
9

SYNOPSIS

11       bind tag ?sequence? ?+??script?
12______________________________________________________________________________
13

INTRODUCTION

15       The  bind  command  associates Tcl scripts with X events.  If all three
16       arguments are specified, bind will arrange for  script  (a  Tcl  script
17       called  the  “binding  script”)  to  be evaluated whenever the event(s)
18       given by sequence occur in the window(s) identified by tag.  If  script
19       is prefixed with a “+”, then it is appended to any existing binding for
20       sequence;  otherwise script replaces any existing binding.   If  script
21       is  an empty string then the current binding for sequence is destroyed,
22       leaving sequence unbound.  In all of the cases where a script  argument
23       is provided, bind returns an empty string.
24
25       If  sequence  is  specified without a script, then the script currently
26       bound to sequence is returned, or an empty string is returned if  there
27       is  no  binding for sequence.  If neither sequence nor script is speci‐
28       fied, then the return value is a list whose elements are  all  the  se‐
29       quences for which there exist bindings for tag.
30
31       The tag argument determines which window(s) the binding applies to.  If
32       tag begins with a dot, as in .a.b.c, then it must be the path name  for
33       a  window; otherwise it may be an arbitrary string.  Each window has an
34       associated list of tags, and a binding applies to a  particular  window
35       if its tag is among those specified for the window.  Although the bind‐
36       tags command may be used to assign an arbitrary set of binding tags  to
37       a window, the default binding tags provide the following behavior:
38
39       •  If  a  tag  is the name of an internal window the binding applies to
40          that window.
41
42       •  If the tag is the name of a toplevel window the binding  applies  to
43          the toplevel window and all its internal windows.
44
45       •  If  the  tag  is the name of a class of widgets, such as Button, the
46          binding applies to all widgets in that class;
47
48       •  If tag has the value all, the binding applies to all windows in  the
49          application.
50

EVENT PATTERNS

52       The  sequence  argument  specifies a sequence of one or more event pat‐
53       terns, with optional white space between the patterns.  Each event pat‐
54       tern  may take one of three forms.  In the simplest case it is a single
55       printing ASCII character, such as a or [.  The character may not  be  a
56       space  character  or  the  character <.  This form of pattern matches a
57       KeyPress event for the particular character.  The second form  of  pat‐
58       tern is longer but more general.  It has the following syntax:
59              <modifier-modifier-type-detail>
60       The  entire  event pattern is surrounded by angle brackets.  Inside the
61       angle brackets are zero or more modifiers, an event type, and an  extra
62       piece  of  information  (detail)  identifying  a  particular  button or
63       keysym.  Any of the fields may be omitted, as long as at least  one  of
64       type  and  detail  is  present.   The fields must be separated by white
65       space or dashes.
66
67       The third form of pattern is used to specify a user-defined, named vir‐
68       tual event.  It has the following syntax:
69              <<name>>
70       The  entire  virtual event pattern is surrounded by double angle brack‐
71       ets.  Inside the angle brackets is the user-defined name of the virtual
72       event.  Modifiers, such as Shift or Control, may not be combined with a
73       virtual event to modify it.  Bindings on a virtual event may be created
74       before the virtual event is defined, and if the definition of a virtual
75       event changes dynamically, all windows bound to that virtual event will
76       respond immediately to the new definition.
77
78       Some  widgets  (e.g. menu and text) issue virtual events when their in‐
79       ternal state is updated in some ways.  Please see the manual  page  for
80       each widget for details.
81
82   MODIFIERS
83       Modifiers consist of any of the following values:
84
85              Control                 Mod1, M1, Command
86              Alt                     Mod2, M2, Option
87              Shift                   Mod3, M3
88              Lock                    Mod4, M4
89              Extended                Mod5, M5
90              Button1, B1             Meta, M
91              Button2, B2             Double
92              Button3, B3             Triple
93              Button4, B4             Quadruple
94              Button5, B5
95
96       Where  more  than  one value is listed, separated by commas, the values
97       are equivalent.  Most of the modifiers have  the  obvious  X  meanings.
98       For example, Button1 requires that button 1 be depressed when the event
99       occurs.  For a binding to match a given event,  the  modifiers  in  the
100       event  must  include  all  of those specified in the event pattern.  An
101       event may also contain additional modifiers not specified in the  bind‐
102       ing.   For  example, if button 1 is pressed while the shift and control
103       keys are down, the pattern <Control-Button-1> will match the event, but
104       <Mod1-Button-1> will not.  If no modifiers are specified, then any com‐
105       bination of modifiers may be present in the event.
106
107       Meta and M refer to whichever of the M1 through M5 modifiers is associ‐
108       ated  with the Meta key(s) on the keyboard (keysyms Meta_R and Meta_L).
109       If there are no Meta keys, or if they are not associated with any modi‐
110       fiers,  then  Meta and M will not match any events.  Similarly, the Alt
111       modifier refers to whichever modifier is associated with the alt key(s)
112       on the keyboard (keysyms Alt_L and Alt_R).
113
114       The Double, Triple and Quadruple modifiers are a convenience for speci‐
115       fying double mouse clicks and other repeated events. They cause a  par‐
116       ticular  event pattern to be repeated 2, 3 or 4 times, and also place a
117       time and space requirement on the sequence: for a sequence of events to
118       match a Double, Triple or Quadruple pattern, all of the events must oc‐
119       cur close together in time and without substantial mouse motion in  be‐
120       tween.  For example, <Double-Button-1> is equivalent to <Button-1><But‐
121       ton-1> with the extra time and space requirement.
122
123       The Command and Option modifiers are equivalents of  Mod1  resp.  Mod2,
124       they correspond to Macintosh-specific modifier keys.
125
126       The  Extended modifier is, at present, specific to Windows.  It appears
127       on events that are associated with the keys on the “extended keyboard”.
128       On a US keyboard, the extended keys include the Alt and Control keys at
129       the right of the keyboard, the cursor keys in the cluster to  the  left
130       of  the  numeric  pad,  the NumLock key, the Break key, the PrintScreen
131       key, and the / and Enter keys in the numeric keypad.
132
133   EVENT TYPES
134       The type field may be any of the standard X event types, with a few ex‐
135       tra  abbreviations.  The type field will also accept a couple non-stan‐
136       dard X event types that were added to better support the Macintosh  and
137       Windows  platforms.   Below is a list of all the valid types; where two
138       names appear together, they are synonyms.
139
140              Activate              Destroy         Map
141              ButtonPress, Button   Enter           MapRequest
142              ButtonRelease         Expose          Motion
143              Circulate             FocusIn         MouseWheel
144              CirculateRequest      FocusOut        Property
145              Colormap              Gravity         Reparent
146              Configure             KeyPress, Key   ResizeRequest
147              ConfigureRequest      KeyRelease      Unmap
148              Create                Leave           Visibility
149              Deactivate
150
151       Most of the above events have the same fields and behaviors  as  events
152       in  the X Windowing system.  You can find more detailed descriptions of
153       these events in any X window programming book.  A couple of the  events
154       are  extensions to the X event system to support features unique to the
155       Macintosh and Windows platforms.  We provide a little  more  detail  on
156       these events here.  These include:
157
158       Activate, Deactivate
159            These  two  events are sent to every sub-window of a toplevel when
160            they change state.  In addition to the focus Window, the Macintosh
161            platform  and  Windows platforms have a notion of an active window
162            (which often has but is not required to have the focus).   On  the
163            Macintosh,  widgets  in the active window have a different appear‐
164            ance than widgets in deactive windows.  The Activate event is sent
165            to  all  the  sub-windows in a toplevel when it changes from being
166            deactive to active.  Likewise, the Deactive event is sent when the
167            window's state changes from active to deactive.  There are no use‐
168            ful percent substitutions you would make  when  binding  to  these
169            events.
170
171       MouseWheel
172            Many  contemporary  mice  support a mouse wheel, which is used for
173            scrolling documents without using the scrollbars.  By rolling  the
174            wheel,  the system will generate MouseWheel events that the appli‐
175            cation can use to scroll.  The event is routed to the window  cur‐
176            rently under the mouse pointer. When the event is received you can
177            use the %D substitution to get the  delta  field  for  the  event,
178            which  is  an  integer  value  describing  how the mouse wheel has
179            moved.  The smallest value for which the system will report is de‐
180            fined by the OS.  The sign of the value determines which direction
181            your widget should scroll.  Positive values should scroll  up  and
182            negative values should scroll down.
183
184            Horizontal  scrolling  uses Shift-MouseWheel events, with positive
185            %D delta substitution indicating left scrolling and negative right
186            scrolling.   Only Windows and macOS Aqua typically fire MouseWheel
187            and Shift-MouseWheel events.  On X11 vertical scrolling is  rather
188            supported  through  Button-4  and  Button-5 events, and horizontal
189            scrolling through Shift-Button-4 and Shift-Button-5 events.  Hori‐
190            zontal  scrolling  events  may  fire  from many different hardware
191            units such as tilt wheels or touchpads.  Horizontal scrolling  can
192            also be emulated by holding Shift and scrolling vertically.
193
194       KeyPress, KeyRelease
195            The KeyPress and KeyRelease events are generated whenever a key is
196            pressed or released.  KeyPress and KeyRelease events are  sent  to
197            the window which currently has the keyboard focus.
198
199       ButtonPress, ButtonRelease, Motion
200            The  ButtonPress  and  ButtonRelease events are generated when the
201            user presses or releases a mouse button.  Motion events are gener‐
202            ated  whenever  the pointer is moved.  ButtonPress, ButtonRelease,
203            and Motion events are normally sent to the window  containing  the
204            pointer.
205
206            When  a mouse button is pressed, the window containing the pointer
207            automatically obtains a temporary pointer grab.   Subsequent  But‐
208            tonPress,  ButtonRelease,  and  Motion events will be sent to that
209            window, regardless of which window contains the pointer, until all
210            buttons have been released.
211
212       Configure
213            A Configure event is sent to a window whenever its size, position,
214            or border width changes, and sometimes when it has  changed  posi‐
215            tion in the stacking order.
216
217       Map, Unmap
218            The  Map and Unmap events are generated whenever the mapping state
219            of a window changes.
220
221            Windows are created in the unmapped state.  Top-level windows  be‐
222            come  mapped when they transition to the normal state, and are un‐
223            mapped in the withdrawn and iconic states.  Other  windows  become
224            mapped  when  they  are placed under control of a geometry manager
225            (for example pack or grid).
226
227            A window is viewable only if it  and  all  of  its  ancestors  are
228            mapped.   Note  that  geometry managers typically do not map their
229            children until they have been mapped  themselves,  and  unmap  all
230            children  when  they  become  unmapped;  hence in Tk Map and Unmap
231            events indicate whether or not a window is viewable.
232
233       Visibility
234            A window is said to be obscured when another window  above  it  in
235            the  stacking  order  fully  or partially overlaps it.  Visibility
236            events are generated whenever a window's obscurity state  changes;
237            the state field (%s) specifies the new state.
238
239       Expose
240            An  Expose  event  is  generated  whenever all or part of a window
241            should be redrawn (for example, when a window is first  mapped  or
242            if  it  becomes  unobscured).   It  is  normally not necessary for
243            client applications to handle Expose events, since Tk handles them
244            internally.
245
246       Destroy
247            A Destroy event is delivered to a window when it is destroyed.
248
249            When the Destroy event is delivered to a widget, it is in a “half-
250            dead” state: the widget still exists, but operations that  involve
251            it may return invalid results, or return an error.
252
253       FocusIn, FocusOut
254            The  FocusIn  and  FocusOut events are generated whenever the key‐
255            board focus changes.  A FocusOut event is sent to  the  old  focus
256            window, and a FocusIn event is sent to the new one.
257
258            In  addition, if the old and new focus windows do not share a com‐
259            mon parent, “virtual crossing” focus events are sent to the inter‐
260            mediate  windows in the hierarchy.  Thus a FocusIn event indicates
261            that the target window or one of its descendants has acquired  the
262            focus,  and  a  FocusOut  event  indicates that the focus has been
263            changed to a window outside the target window's hierarchy.
264
265            The keyboard focus may be changed explicitly by a call  to  focus,
266            or implicitly by the window manager.
267
268       Enter, Leave
269            An  Enter  event  is sent to a window when the pointer enters that
270            window, and a Leave event is sent when the pointer leaves it.
271
272            If there is a pointer grab in effect, Enter and Leave  events  are
273            only delivered to the window owning the grab.
274
275            In addition, when the pointer moves between two windows, Enter and
276            Leave “virtual crossing” events are sent to  intermediate  windows
277            in  the  hierarchy  in the same manner as for FocusIn and FocusOut
278            events.
279
280       Property
281            A Property event is sent to a window whenever an  X  property  be‐
282            longing to that window is changed or deleted.  Property events are
283            not normally delivered to Tk applications as they are  handled  by
284            the Tk core.
285
286       Colormap
287            A  Colormap  event  is  generated whenever the colormap associated
288            with a window has been changed, installed, or uninstalled.
289
290            Widgets may be assigned a private colormap by specifying  a  -col‐
291            ormap option; the window manager is responsible for installing and
292            uninstalling colormaps as necessary.
293
294            Note that Tk provides no useful details for this event type.
295
296       MapRequest, CirculateRequest, ResizeRequest, ConfigureRequest, Create
297            These events are not normally delivered to Tk applications.   They
298            are  included  for  completeness, to make it possible to write X11
299            window managers in Tk.  (These events are only  delivered  when  a
300            client  has  selected SubstructureRedirectMask on a window; the Tk
301            core does not use this mask.)
302
303       Gravity, Reparent, Circulate
304            The events Gravity and Reparent are not normally delivered  to  Tk
305            applications.  They are included for completeness.
306
307            A  Circulate  event indicates that the window has moved to the top
308            or to the bottom of the stacking order as a result of  an  XCircu‐
309            lateSubwindows protocol request.  Note that the stacking order may
310            be changed for other reasons which do  not  generate  a  Circulate
311            event, and that Tk does not use XCirculateSubwindows() internally.
312            This event type is included only for completeness; there is no re‐
313            liable way to track changes to a window's position in the stacking
314            order.
315
316   EVENT DETAILS
317       The last part of a long event specification is detail.  In the case  of
318       a  ButtonPress  or  ButtonRelease  event,  it is the number of a button
319       (1-5).  If a button number is given, then only an event on that partic‐
320       ular button will match;  if no button number is given, then an event on
321       any button will match.  Note:  giving a specific button number is  dif‐
322       ferent  than specifying a button modifier; in the first case, it refers
323       to a button being pressed or released, while in the second it refers to
324       some other button that is already depressed when the matching event oc‐
325       curs.  If a button number is given then type may be omitted:   if  will
326       default  to  ButtonPress.  For example, the specifier <1> is equivalent
327       to <ButtonPress-1>.
328
329       If the event type is KeyPress or KeyRelease, then detail may be  speci‐
330       fied  in  the  form of an X keysym.  Keysyms are textual specifications
331       for particular keys on the keyboard; they include all the  alphanumeric
332       ASCII characters (e.g.  “a” is the keysym for the ASCII character “a”),
333       plus descriptions for non-alphanumeric characters (“comma”is the keysym
334       for  the comma character), plus descriptions for all the non-ASCII keys
335       on the keyboard (e.g.  “Shift_L” is the keysym for the left shift  key,
336       and  “F1”  is  the  keysym for the F1 function key, if it exists).  The
337       complete list of keysyms is not presented here;   it  is  available  in
338       other  X  documentation  and may vary from system to system.  If neces‐
339       sary, you can use the %K notation described  below  to  print  out  the
340       keysym  name  for  a particular key.  If a keysym detail is given, then
341       the type field may be omitted;  it will default to KeyPress.  For exam‐
342       ple, <Control-comma> is equivalent to <Control-KeyPress-comma>.
343

BINDING SCRIPTS AND SUBSTITUTIONS

345       The  script  argument  to  bind  is  a  Tcl script, called the “binding
346       script”, which will be executed whenever the given event  sequence  oc‐
347       curs.   Command  will be executed in the same interpreter that the bind
348       command was executed in, and it will run at global level  (only  global
349       variables  will  be  accessible).  If script contains any % characters,
350       then the script will not be executed directly.  Instead, a  new  script
351       will  be generated by replacing each %, and the character following it,
352       with information from the current event.  The  replacement  depends  on
353       the  character  following  the %, as defined in the list below.  Unless
354       otherwise indicated, the replacement string is the decimal value of the
355       given field from the current event.  Some of the substitutions are only
356       valid for certain types of events;  if they are used for other types of
357       events the value substituted is undefined.
358
359       %%   Replaced with a single percent.
360
361       %#   The number of the last client request processed by the server (the
362            serial field from the event).  Valid for all event types.
363
364       %a   The above field from the event, formatted as a hexadecimal number.
365            Valid only for Configure events.  Indicates the sibling window im‐
366            mediately below the receiving window in the stacking order,  or  0
367            if the receiving window is at the bottom.
368
369       %b   The number of the button that was pressed or released.  Valid only
370            for ButtonPress and ButtonRelease events.
371
372       %c   The count field from the event.  Valid  only  for  Expose  events.
373            Indicates  that  there  are count pending Expose events which have
374            not yet been delivered to the window.
375
376       %d   The detail or user_data field from the event.  The %d is  replaced
377            by  a  string  identifying the detail.  For Enter, Leave, FocusIn,
378            and FocusOut events, the string will be one of the following:
379
380                   NotifyAncestor          NotifyNonlinearVirtual
381                   NotifyDetailNone        NotifyPointer
382                   NotifyInferior          NotifyPointerRoot
383                   NotifyNonlinear         NotifyVirtual
384
385            For ConfigureRequest events, the string will be one of:
386
387                   Above                   Opposite
388                   Below                   None
389                   BottomIf                TopIf
390
391            For virtual events, the string will be whatever value is stored in
392            the  user_data  field  when  the event was created (typically with
393            event generate), or the empty string if the field is  NULL.   Vir‐
394            tual  events  corresponding to key sequence presses (see event add
395            for details) set the user_data to NULL.   For  events  other  than
396            these, the substituted string is undefined.
397
398       %f   The focus field from the event (0 or 1).  Valid only for Enter and
399            Leave events.  1 if the receiving window is the focus window or  a
400            descendant of the focus window, 0 otherwise.
401
402       %h   The height field from the event.  Valid for the Configure, Config‐
403            ureRequest, Create, ResizeRequest, and Expose  events.   Indicates
404            the new or requested height of the window.
405
406       %i   The  window field from the event, represented as a hexadecimal in‐
407            teger.  Valid for all event types.
408
409       %k   The keycode field from the event.  Valid  only  for  KeyPress  and
410            KeyRelease events.
411
412       %m   The  mode  field from the event.  The substituted string is one of
413            NotifyNormal,  NotifyGrab,  NotifyUngrab,  or  NotifyWhileGrabbed.
414            Valid only for Enter, FocusIn, FocusOut, and Leave events.
415
416       %o   The  override_redirect  field from the event.  Valid only for Map,
417            Reparent, and Configure events.
418
419       %p   The place field from the event, substituted as one of the  strings
420            PlaceOnTop  or PlaceOnBottom.  Valid only for Circulate and Circu‐
421            lateRequest events.
422
423       %s   The state field from the event.  For  ButtonPress,  ButtonRelease,
424            Enter,  KeyPress,  KeyRelease, Leave, and Motion events, a decimal
425            string is substituted.  For Visibility, one of the  strings  Visi‐
426            bilityUnobscured,  VisibilityPartiallyObscured, and VisibilityFul‐
427            lyObscured is substituted.  For Property events, substituted  with
428            either  the string NewValue (indicating that the property has been
429            created or modified) or Delete (indicating that the  property  has
430            been removed).
431
432       %t   The  time  field  from  the event.  This is the X server timestamp
433            (typically the time since the last server reset) in  milliseconds,
434            when the event occurred.  Valid for most events.
435
436       %w   The  width  field  from the event.  Indicates the new or requested
437            width of the window.  Valid only for Configure,  ConfigureRequest,
438            Create, ResizeRequest, and Expose events.
439
440       %x, %y
441            The  x  and  y  fields from the event.  For ButtonPress, ButtonRe‐
442            lease, Motion, KeyPress, KeyRelease, and MouseWheel events, %x and
443            %y  indicate the position of the mouse pointer relative to the re‐
444            ceiving window.  For key events on the Macintosh these are the co‐
445            ordinates  of the mouse at the moment when an X11 KeyEvent is sent
446            to Tk, which could be slightly later than the time of the physical
447            press  or release.  For Enter and Leave events, the position where
448            the mouse pointer crossed the window, relative  to  the  receiving
449            window.   For  Configure  and Create requests, the x and y coordi‐
450            nates of the window relative to its parent window.
451
452       %A   Substitutes the UNICODE character corresponding to the  event,  or
453            the  empty  string  if  the event does not correspond to a UNICODE
454            character (e.g. the shift key was  pressed).  On  X11,  XmbLookup‐
455            String  (or XLookupString when input method support is turned off)
456            does all the work of translating from the event to a UNICODE char‐
457            acter.   On  X11,  valid  only  for KeyPress event. On Windows and
458            macOS/aqua, valid only for KeyPress and KeyRelease events.
459
460       %B   The border_width field from the event.  Valid only for  Configure,
461            ConfigureRequest, and Create events.
462
463       %D   This  reports  the  delta  value of a MouseWheel event.  The delta
464            value represents the rotation  units  the  mouse  wheel  has  been
465            moved.  The  sign  of the value represents the direction the mouse
466            wheel was scrolled.
467
468       %E   The send_event field from the event.  Valid for all  event  types.
469            0  indicates that this is a “normal” event, 1 indicates that it is
470            a “synthetic” event generated by SendEvent.
471
472       %K   The keysym corresponding to the event, substituted  as  a  textual
473            string.  Valid only for KeyPress and KeyRelease events.
474
475       %M   The number of script-based binding patterns matched so far for the
476            event.  Valid for all event types.
477
478       %N   The keysym corresponding to the event, substituted  as  a  decimal
479            number.  Valid only for KeyPress and KeyRelease events.
480
481       %P   The  name  of  the property being updated or deleted (which may be
482            converted to an XAtom using winfo atom.) Valid only  for  Property
483            events.
484
485       %R   The  root window identifier from the event.  Valid only for events
486            containing a root field.
487
488       %S   The subwindow window identifier from the  event,  formatted  as  a
489            hexadecimal  number.  Valid only for events containing a subwindow
490            field.
491
492       %T   The type field from the event.  Valid for all event types.
493
494       %W   The path name of the window to which the event was  reported  (the
495            window field from the event).  Valid for all event types.
496
497       %X, %Y
498            The  x_root  and  y_root fields from the event.  If a virtual-root
499            window manager is being used then the substituted values  are  the
500            corresponding  x-coordinate  and y-coordinate in the virtual root.
501            Valid only for ButtonPress, ButtonRelease, Enter, KeyPress, KeyRe‐
502            lease, Leave and Motion events.  Same meaning as %x and %y, except
503            relative to the (virtual) root window.
504
505       The replacement string for a %-replacement is formatted as a proper Tcl
506       list  element.   This means that spaces or special characters such as $
507       and { may be preceded by backslashes.  This guarantees that the  string
508       will be passed through the Tcl parser when the binding script is evalu‐
509       ated.  Most replacements are numbers or well-defined  strings  such  as
510       Above;  for these replacements no special formatting is ever necessary.
511       The most common case where reformatting occurs is for the %A  substitu‐
512       tion.  For example, if script is
513              insert %A
514       and  the character typed is an open square bracket, then the script ac‐
515       tually executed will be
516              insert \[
517       This will cause the insert to receive the original  replacement  string
518       (open  square  bracket)  as its first argument.  If the extra backslash
519       had not been added, Tcl would not have been able to  parse  the  script
520       correctly.
521

MULTIPLE MATCHES

523       It  is  possible for several bindings to match a given X event.  If the
524       bindings are associated with different tag's, then each of the bindings
525       will  be executed, in order.  By default, a binding for the widget will
526       be executed first, followed by a  class  binding,  a  binding  for  its
527       toplevel,  and  an  all  binding.   The bindtags command may be used to
528       change this order for a particular window or  to  associate  additional
529       binding tags with the window.
530
531       The  continue and break commands may be used inside a binding script to
532       control the processing of matching scripts.   If  continue  is  invoked
533       within  a binding script, then this binding script, including all other
534       “+” appended scripts, is terminated but  Tk  will  continue  processing
535       binding  scripts  associated with other tag's.  If the break command is
536       invoked within a binding script, then that  script  terminates  and  no
537       other scripts will be invoked for the event.
538
539       Within  a script called from the binding script, return -code ok may be
540       used to continue processing (including “+” appended scripts), or return
541       -code break may be used to stop processing all other binding scripts.
542
543       If  more  than one binding matches a particular event and they have the
544       same tag, then the most specific binding is chosen and  its  script  is
545       evaluated.   The  following  tests  are applied, in order, to determine
546       which of several matching sequences is more specific:
547
548              (a)    an event pattern that specifies a specific button or  key
549                     is more specific than one that does not;
550
551              (b)    a  longer sequence (in terms of number of events matched)
552                     is more specific than a shorter sequence;
553
554              (c)    if the modifiers specified in one pattern are a subset of
555                     the  modifiers  in another pattern, then the pattern with
556                     more modifiers is more specific;
557
558              (d)    a virtual event whose physical pattern  matches  the  se‐
559                     quence  is  less  specific than the same physical pattern
560                     that is not associated with a virtual event;
561
562              (e)    given a sequence that matches two or more virtual events,
563                     one  of  the virtual events will be chosen, but the order
564                     is undefined.
565
566       If the matching sequences contain  more  than  one  event,  then  tests
567       (c)-(e)  are  applied  in order from the most recent event to the least
568       recent event in the sequences.  If these tests fail to determine a win‐
569       ner, then the most recently registered sequence is the winner.
570
571       If  there  are  two (or more) virtual events that are both triggered by
572       the same sequence, and both of those virtual events are  bound  to  the
573       same window tag, then only one of the virtual events will be triggered,
574       and it will be picked at random:
575              event add <<Paste>> <Control-y>
576              event add <<Paste>> <Button-2>
577              event add <<Scroll>> <Button-2>
578              bind Entry <<Paste>> {puts Paste}
579              bind Entry <<Scroll>> {puts Scroll}
580       If the user types Control-y, the <<Paste>> binding will be invoked, but
581       if  the  user  presses button 2 then one of either the <<Paste>> or the
582       <<Scroll>> bindings will be invoked, but exactly which one gets invoked
583       is undefined.
584
585       If  an  X  event  does not match any of the existing bindings, then the
586       event is ignored.  An unbound event is not considered to be an error.
587

MULTI-EVENT SEQUENCES AND IGNORED EVENTS

589       When a sequence specified in a bind  command  contains  more  than  one
590       event  pattern,  then its script is executed whenever the recent events
591       (leading up to and including the current event)  match  the  given  se‐
592       quence.   This  means, for example, that if button 1 is clicked repeat‐
593       edly the sequence <Double-ButtonPress-1> will match each  button  press
594       but  the  first.  If extraneous events that would prevent a match occur
595       in the middle of an event sequence then the extraneous events  are  ig‐
596       nored  unless  they  are  KeyPress or ButtonPress events.  For example,
597       <Double-ButtonPress-1> will match a sequence of presses  of  button  1,
598       even  though  there  will  be ButtonRelease events (and possibly Motion
599       events) between the ButtonPress events.  Furthermore, a KeyPress  event
600       may  be  preceded  by  any number of other KeyPress events for modifier
601       keys without the modifier keys preventing a match.   For  example,  the
602       event  sequence  aB will match a press of the a key, a release of the a
603       key, a press of the Shift key, and a press of the b key:  the press  of
604       Shift is ignored because it is a modifier key.  Finally, if several Mo‐
605       tion events occur in a row, only the last one is used for  purposes  of
606       matching binding sequences.
607

ERRORS

609       If an error occurs in executing the script for a binding then the bger‐
610       ror mechanism is used to report the error.  The bgerror command will be
611       executed at global level (outside the context of any Tcl procedure).
612

EXAMPLES

614       Arrange  for  a string describing the motion of the mouse to be printed
615       out when the mouse is double-clicked:
616              bind . <Double-1> {
617                  puts "hi from (%x,%y)"
618              }
619
620       A little GUI that displays what the keysym name of the last key pressed
621       is:
622              set keysym "Press any key"
623              pack [label .l -textvariable keysym -padx 2m -pady 1m]
624              bind . <Key> {
625                  set keysym "You pressed %K"
626              }
627

SEE ALSO

629       bgerror(n), bindtags(n), event(n), focus(n), grab(n), keysyms(n)
630

KEYWORDS

632       binding, event
633
634
635
636Tk                                    8.0                              bind(n)
Impressum