bindtags

bindtags , a Tk command, manages which bindings apply to a window, and order of their evaluation.

Synopsis

bindtags window ?tagList?

Documentation

man page
Introduction to Bindtags
by Bryan Oakley, 2006-03

See Also

tedit
base for a Tcl editor, using bindtags

Examples

Playing with bits
Creates a widget in a frame and adds, at index 1 of each element in the frame, a tag named after the namespace associated with the widget instance. This lays a good framework for delegating evens to widgets.
A tiny input manager

Description

Used to define the binding hierarchy for Widgets.

RS gave a great description of bindtags: he called them "bundles of bindings" or something to that effect. Widgets have a list of such "bundles" associated with them. Every time an event happens over that widget, each "bundle" is checked in turn, and if there is a binding matching the event, it is fired. If the binding does a break no more "bundles" are considered. Otherwise, each additional bundle goes through the same processing in turn.

By default, each widget has a set of bindtags that includes the specific widget, the widget class, the toplevel window for that widget, and the special word "all". So, for example, to attach a binding to all widgets you can associate the binding with the tag "all" rather than a specific widget.

Example: Uppercase Entry

Here is a little example that diverts lowercase letter keys to their uppercase variants (other characters come through unharmed) - for the "; break" bit I had to quote the binding body, instead of listifying it as one normally should:

foreach i {a b c d e f g h i j k l m n o p q r s t u v w x y z} {
    bind Upcase $i "event generate %W [string toupper $i]; break"
} ;# RS
# Usage example, associate the bindtags to a widget:
pack [entry .e]
bindtags    .e [concat Upcase [bindtags .e]]

Example, KHIM

"... KHIM is a good example. Any widget that wants KHIM's services can add 'khim' to its list of bindtags, and get a whole lot of <Key> bindings ... What you do to get KHIM to apply to a text widget is to say,

bindtags $text [linsert [bindtags $text] 1 khim]

... which will change the bindtags from {.text Text . all} to {.text khim Text . all}"

Misc

vr: WRT bindtags, can you explain how to preserve the original code bound to that event using bindtags? Also, why should I be binding to the buttonrelease event?

RS: Simply: if you have custom bindings, bind them to a bindtag, not a widget name; place that bindtag (with the bindtags command) in the binding sequence of the widget(s) in question.

eval bindtags $myWidget myBindings [bindtags $mywidget]

puts everything that you bind to myBindings before all other bindings of myWidget (but they are still executed, if yours doesn't [[break])