Multiple Matches

It is possible for several bindings to match a given X event. If the bindings are associated with different tags, then each of the bindings will be executed, in order. By default, a binding for the widget will be executed first, followed by a class binding, a binding for its toplevel, and an all binding. The tk/bindtags command may be used to change this order for a particular window or to associate additional binding tags with the window.

The continue and break commands may be used to control the processing of matching scripts. If continue is invoked, then the current binding script is terminated but Tk will continue processing binding scripts associated with other tags. If the break command is invoked within a binding script, then that script terminates and no other scripts will be invoked for the event.

Note that break and continue can only be invoked as toplevel-commands but not from inside a lambda expression.

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:

  1. an event pattern that specifies a specific button or key is more specific than one that doesn't;
  2. a longer sequence (in terms of number of events matched) is more specific than a shorter sequence;
  3. 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.
  4. 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.
  5. 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 3-5 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 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:

(tk/event 'add '<<Paste>> '<Control-y>)
(tk/event 'add '<<Paste>> '<Button-2>
(tk/event 'add '<<Scroll>> '<Button-2>
(tk/bind 'Entry '<<Paste>> (lambda () (print 'Paste)))
(tk/bind 'Entry '<<Scroll>> (lambda () (print '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, but exactly which one gets invoked is undefined.

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.


© Author | Home | Sitemap