Example 8.2. Event Handler

button .b

bind .b <1> {
   rtl::debug stdout \
       "An Event Handler for »1«."

Example 8.2, “Event Handler”, shows how the generated code for a window specific event handler may look like. The Tcl/Tk command »bind« adds an event handler to a set.

The Inspector context menu presents the following scripts:

%W %x %y;# widget (x,y)-pos
%W %x %y %X %Y;# widget rel+abs (x,y)-pos
%W %s %x %y;# widget -state (x,y)-pos
%W %A;# widget key code

These event templates are stored inside the global preferences under ::rat(settings,Preset,Event,Parameter). »%s« is the current state of the event generating window. »%A« is the key-code of the keyboard key pressed.

Prefer the %W place holder notation over window path names, when designing an event handler. Using window path names inside of event handlers requires to encapsulate the event handler either with list or subst . An explanation for list and subst was given in Property Encapsulation.

The Tcl-command »break« has a special meaning inside of an event handler. »break« terminates the processing of the current event. That is: no other event handler inside a later set is evaluated. See Example 8.3, “break inside Events”. The bindtags list determines the sequence in which event handler sets are evaluated; direction is from left to right.

Example 8.3. break inside Events

pack [button .b]

# The event handlers:
bindtags .b {.b b_1 b_2}
bind b_2 <1>  {rtl::debug stderr "not called"}
bind b_1 <1>  break
bind .b <1>  {rtl::debug stdout "%W is called"}

# A synthetic event is generated for ».b«
event generate .b <1>