1bind(3) User Contributed Perl Documentation bind(3)
2
3
4
6 Tk::bind - Arrange for X events to invoke callbacks
7
9 Retrieve bindings:
10
11 $widget->bind
12
13 $widget->bind(tag)
14
15 $widget->bind(sequence)
16
17 $widget->bind(tag,sequence)
18
19 Associate and destroy bindings:
20
21 $widget->bind(sequence,callback)
22
23 $widget->bind(tag,sequence,callback)
24
26 The bind method associates callbacks with X events. If callback is
27 specified, bind will arrange for callback to be evaluated whenever the
28 event(s) given by sequence occur in the window(s) identified by $widget
29 or tag. If callback is an empty string then the current binding for
30 sequence is destroyed, leaving sequence unbound. In all of the cases
31 where a callback argument is provided, bind returns an empty string.
32
33 If sequence is specified without a callback, then the callback cur‐
34 rently bound to sequence is returned, or undef is returned if there is
35 no binding for sequence. If neither sequence nor callback is speci‐
36 fied, then the return value is a list whose elements are all the
37 sequences for which there exist bindings for tag.
38
39 If no tag is specified then the bind refers to $widget. If tag is
40 specified then it is typically a class name and the bind refers to all
41 instances of the class on the MainWindow associated with $widget. (It
42 is possible for tag to be another "widget object" but this practice is
43 deprecated.) Perl's ref($object) can be used to get the class name of
44 any object. Each window has an associated list of tags, and a binding
45 applies to a particular window if its tag is among those specified for
46 the window. Although the bindtags method may be used to assign an
47 arbitrary set of binding tags to a window, the default binding tags
48 provide the following behavior:
49
50 If a tag is the name of an internal window the binding applies to that
51 window.
52
53 If the tag is the name of a toplevel window the binding applies to the
54 toplevel window and all its internal windows.
55
56 If the tag is the name of a class of widgets, such as Tk::Button, the
57 binding applies to all widgets in that class;
58
59 If tag has the value all, the binding applies to all windows descended
60 from the MainWindow of the application.
61
63 The sequence argument specifies a sequence of one or more event pat‐
64 terns, with optional white space between the patterns. Each event pat
65 may take one of three forms. In the simplest case it is a single
66 printing ASCII character, such as a or [. The character may not be a
67 space character or the character <. This form of pattern matches a
68 KeyPress event for the particular character. The second form of pat‐
69 tern is longer but more general. It has the following syntax:
70
71 '<modifier-modifier-type-detail>'
72
73 The entire event pattern is surrounded by angle brackets, and normally
74 needs to be quoted, as angle brackets are special to perl. Inside the
75 angle brackets are zero or more modifiers, an event type, and an extra
76 piece of information (detail) identifying a particular button or
77 keysym. Any of the fields may be omitted, as long as at least one of
78 type and detail is present. The fields must be separated by white
79 space or dashes.
80
81 The third form of pattern is used to specify a user-defined, named vir‐
82 tual event; see Tk::event for details. It has the following syntax:
83
84 '<<name>>'
85
86 The entire virtual event pattern is surrounded by double angle brack‐
87 ets. Inside the angle brackets is the user-defined name of the virtual
88 event. Modifiers, such as Shift or Control, may not be combined with a
89 virtual event to modify it. Bindings on a virtual event may be created
90 before the virtual event is defined, and if the definition of a virtual
91 event changes dynamically, all windows bound to that virtual event will
92 respond immediately to the new definition.
93
95 Modifiers consist of any of the following values:
96
97 Control Mod2, M2
98 Shift Mod3, M3
99 Lock Mod4, M4
100 Button1, B1 Mod5, M5
101 Button2, B2 Meta, M
102 Button3, B3 Alt
103 Button4, B4 Double
104 Button5, B5 Triple
105 Mod1, M1 Quadruple
106
107 Where more than one value is listed, separated by commas, the values
108 are equivalent. Most of the modifiers have the obvious X meanings.
109 For example, Button1 requires that button 1 be depressed when the event
110 occurs. For a binding to match a given event, the modifiers in the
111 event must include all of those specified in the event pattern. An
112 event may also contain additional modifiers not specified in the bind‐
113 ing. For example, if button 1 is pressed while the shift and control
114 keys are down, the pattern <Control-Button-1> will match the event, but
115 <Mod1-Button-1> will not. If no modifiers are specified, then any com‐
116 bination of modifiers may be present in the event.
117
118 Meta and M refer to whichever of the M1 through M5 modifiers is associ‐
119 ated with the meta key(s) on the keyboard (keysyms Meta_R and Meta_L).
120 If there are no meta keys, or if they are not associated with any modi‐
121 fiers, then Meta and M will not match any events. Similarly, the Alt
122 modifier refers to whichever modifier is associated with the alt key(s)
123 on the keyboard (keysyms Alt_L and Alt_R).
124
125 The Double, Triple and Quadruple modifiers are a convenience for speci‐
126 fying double mouse clicks and other repeated events. They cause a par‐
127 ticular event pattern to be repeated 2, 3 or 4 times, and also place a
128 time and space requirement on the sequence: for a sequence of events to
129 match a Double, Triple or Quadruple pattern, all of the events must
130 occur close together in time and without substantial mouse motion in
131 between. For example, <Double-Button-1> is equivalent to <But‐
132 ton-1><Button-1> with the extra time and space requirement.
133
135 The type field may be any of the standard X event types, with a few
136 extra abbreviations. Below is a list of all the valid types; where two
137 names appear together, they are synonyms.
138
139 Activate Destroy Map
140 ButtonPress, Button Enter MapRequest
141 ButtonRelease Expose Motion
142 Circulate FocusIn MouseWheel
143 CirculateRequest FocusOut Property
144 Colormap Gravity Reparent
145 Configure KeyPress, Key ResizeRequest
146 ConfigureRequest KeyRelease Unmap
147 Create Leave Visibility
148 Deactivate
149
150 Most of the above events have the same fields and behaviors as events
151 in the X Windowing system. You can find more detailed descriptions of
152 these events in any X window programming book. A couple of the events
153 are extensions to the X event system to support features unique to the
154 Macintosh and Windows platforms. We provide a little more detail on
155 these events here. These include:
156
157 Activate
158 Deactivate
159
160 These two events are sent to every sub-window of a toplevel when they
161 change state. In addition to the focus Window, the Macintosh platform
162 and Windows platforms have a notion of an active window (which often
163 has but is not required to have the focus). On the Macintosh, widgets
164 in the active window have a different appearance than widgets in deac‐
165 tive windows. The Activate event is sent to all the sub-windows in a
166 toplevel when it changes from being deactive to active. Likewise, the
167 Deactive event is sent when the window's state changes from active to
168 deactive. There are no use- ful percent substitutions you would make
169 when binding to these events.
170
171 MouseWheel
172
173 Some mice on the Windows platform support a mouse wheel which is used
174 for scrolling documents without using the scrollbars. By rolling
175 the wheel, the system will generate MouseWheel events that the appli‐
176 cation can use to scroll. Like Key events the event is always routed
177 to the window that currently has focus. When the event is received
178 you can use the %D substitution to get the delta field for the event
179 which is a integer value of motion that the mouse wheel has
180 moved. The smallest value for which the system will report is defined
181 by the OS. On Windows 95 & 98 machines this value is at least 120
182 before it is reported. However, higher resolution devices may be
183 available in the future. The sign of the value determines which
184 direction your widget should scroll. Positive values should scroll up
185 and negative values should scroll down.
186
187 The last part of a long event specification is detail. In the case of
188 a ButtonPress or ButtonRelease event, it is the number of a button
189 (1-5). If a button number is given, then only an event on that partic‐
190 ular button will match; if no button number is given, then an event on
191 any button will match. Note: giving a specific button number is dif‐
192 ferent than specifying a button modifier; in the first case, it refers
193 to a button being pressed or released, while in the second it refers to
194 some other button that is already depressed when the matching event
195 occurs. If a button number is given then type may be omitted: if will
196 default to ButtonPress. For example, the specifier <1> is equivalent
197 to <ButtonPress-1>.
198
199 If the event type is KeyPress or KeyRelease, then detail may be speci‐
200 fied in the form of an X keysym. Keysyms are textual specifications
201 for particular keys on the keyboard; they include all the alphanumeric
202 ASCII characters (e.g. ``a'' is the keysym for the ASCII character
203 ``a''), plus descriptions for non-alphanumeric characters (``comma'' is
204 the keysym for the comma character), plus descriptions for all the non-
205 ASCII keys on the keyboard (``Shift_L'' is the keysm for the left shift
206 key, and ``F1'' is the keysym for the F1 function key, if it exists).
207 The complete list of keysyms is not presented here; it is available in
208 other X documentation and may vary from system to system. If neces‐
209 sary, you can use the 'K' notation described below to print out the
210 keysym name for a particular key. If a keysym detail is given, then
211 the type field may be omitted; it will default to KeyPress. For exam‐
212 ple, <Control-comma> is equivalent to <Control-KeyPress-comma>.
213
215 The callback argument to bind is a perl/Tk callback. which will be
216 executed whenever the given event sequence occurs. (See Tk::callbacks
217 for description of the possible forms.) Callback will be associated
218 with the same MainWindow that is associated with the $widget that was
219 used to invoke the bind method, and it will run as though called from
220 MainLoop. If callback contains any Ev(%) calls, then each "nested"
221 Ev(%) "callback" will be evaluated when the event occurs to form argu‐
222 ments to be passed to the main callback. The replacement depends on
223 the character %, as defined in the list below. Unless otherwise indi‐
224 cated, the replacement string is the numeric (decimal) value of the
225 given field from the current event. Perl/Tk has enhanced this mechanism
226 slightly compared to the comparable Tcl/Tk mechanism. The enhancements
227 are not yet all reflected in the list below. Some of the substitutions
228 are only valid for certain types of events; if they are used for other
229 types of events the value substituted is undefined (not the same as
230 undef!).
231
232 '#' The number of the last client request processed by the server (the
233 serial field from the event). Valid for all event types.
234
235 'a' The above field from the event, formatted as a hexadecimal number.
236 Valid only for Configure events.
237
238 'b' The number of the button that was pressed or released. Valid only
239 for ButtonPress and ButtonRelease events.
240
241 'c' The count field from the event. Valid only for Expose events.
242
243 'd' The detail field from the event. The 'd' is replaced by a string
244 identifying the detail. For Enter, Leave, FocusIn, and FocusOut
245 events, the string will be one of the following:
246
247 NotifyAncestor NotifyNonlinearVirtual
248 NotifyDetailNone NotifyPointer
249 NotifyInferior NotifyPointerRoot
250 NotifyNonlinear NotifyVirtual
251
252 For ConfigureRequest events, the string will be one of:
253
254 Above Opposite
255 Below None
256 BottomIf TopIf
257
258 For events other than these, the substituted string is undefined.
259 (Note that this is not the same as Detail part of sequence use to
260 specify the event.)
261
262 'f' The focus field from the event (0 or 1). Valid only for Enter and
263 Leave events.
264
265 'h' The height field from the event. Valid only for Configure, Config‐
266 ureRequest, Create, Expose, and ResizeRequest events.
267
268 'i' The window field from the event, represented as a hexadecimal
269 integer.
270
271 'k' The keycode field from the event. Valid only for KeyPress and
272 KeyRelease events.
273
274 'm' The mode field from the event. The substituted string is one of
275 NotifyNormal, NotifyGrab, NotifyUngrab, or NotifyWhileGrabbed.
276 Valid only for Enter, FocusIn, FocusOut, and Leave events.
277
278 'o' The override_redirect field from the event. Valid only for Map,
279 Reparent, and Configure events.
280
281 'p' The place field from the event, substituted as one of the strings
282 PlaceOnTop or PlaceOnBottom. Valid only for Circulate and Circu‐
283 lateRequest events.
284
285 's' The state field from the event. For ButtonPress, ButtonRelease,
286 Enter, KeyPress, KeyRelease, Leave, and Motion events, a decimal
287 string is substituted. For Visibility, one of the strings Visibil‐
288 ityUnobscured, VisibilityPartiallyObscured, and VisibilityFullyOb‐
289 scured is substituted.
290
291 't' The time field from the event. Valid only for events that contain
292 a time field.
293
294 'w' The width field from the event. Valid only for Configure, Con‐
295 figueRequest, Create, Expose, and ResizeREquest events.
296
297 'x' The x field from the event. Valid only for events containing an x
298 field.
299
300 'y' The y field from the event. Valid only for events containing a y
301 field.
302
303 '@' The string "@x,y" where x and y are as above. Valid only for
304 events containing x and y fields. This format is used my methods
305 of Tk::Text and similar widgets.
306
307 'A' Substitutes the UNICODE character corresponding to the event, or
308 the empty string if the event doesn't correspond to a UNICODE char‐
309 acter (e.g. the shift key was pressed). XmbLookupString does all
310 the work of translating from the event to a UNICODE character.
311 Valid only for KeyPress and KeyRelease events.
312
313 'B' The border_width field from the event. Valid only for Configure,
314 ConfigureRequest and Create events.
315
316 'D' This reports the delta value of a MouseWheel event. The delta
317 value represents the rotation units the mouse wheel has been
318 moved. On Windows 95 & 98 systems the smallest value for the
319 delta is 120. Future systems may support higher resolution values
320 for the delta. The sign of the value represents the direction the
321 mouse wheel was scrolled.
322
323 'E' The send_event field from the event. Valid for all event types.
324
325 'K' The keysym corresponding to the event, substituted as a textual
326 string. Valid only for KeyPress and KeyRelease events.
327
328 'N' The keysym corresponding to the event, substituted as a decimal
329 number. Valid only for KeyPress and KeyRelease events.
330
331 'R' The root window identifier from the event. Valid only for events
332 containing a root field.
333
334 'S' The subwindow window identifier from the event, as an object if it
335 is one otherwise as a hexadecimal number. Valid only for events
336 containing a subwindow field.
337
338 'T' The type field from the event. Valid for all event types.
339
340 'W' The window to which the event was reported (the $widget field from
341 the event) - as an perl/Tk object. Valid for all event types.
342
343 'X' The x_root field from the event. If a virtual-root window manager
344 is being used then the substituted value is the corresponding
345 x-coordinate in the virtual root. Valid only for ButtonPress, But‐
346 tonRelease, KeyPress, KeyRelease, and Motion events.
347
348 'Y' The y_root field from the event. If a virtual-root window manager
349 is being used then the substituted value is the corresponding
350 y-coordinate in the virtual root. Valid only for ButtonPress, But‐
351 tonRelease, KeyPress, KeyRelease, and Motion events.
352
354 It is possible for several bindings to match a given X event. If the
355 bindings are associated with different tag's, then each of the bindings
356 will be executed, in order. By default, a class binding will be exe‐
357 cuted first, followed by a binding for the widget, a binding for its
358 toplevel, and an all binding. The bindtags method may be used to
359 change this order for a particular window or to associate additional
360 binding tags with the window.
361
362 return and Tk->break may be used inside a callback to control the pro‐
363 cessing of matching callbacks. If return is invoked, then the current
364 callback is terminated but Tk will continue processing callbacks asso‐
365 ciated with other tag's. If Tk->break is invoked within a callback,
366 then that callback terminates and no other callbacks will be invoked
367 for the event. (Tk->break is implemented via perl's die with a special
368 value which is "caught" by the perl/Tk "glue" code.)
369
370 If more than one binding matches a particular event and they have the
371 same tag, then the most specific binding is chosen and its callback is
372 evaluated. The following tests are applied, in order, to determine
373 which of several matching sequences is more specific: (a) an event pat‐
374 tern that specifies a specific button or key is more specific than one
375 that doesn't; (b) a longer sequence (in terms of number of events
376 matched) is more specific than a shorter sequence; (c) if the modifiers
377 specified in one pattern are a subset of the modifiers in another pat‐
378 tern, then the pattern with more modifiers is more specific. (d) a
379 virtual event whose physical pattern matches the sequence is less spe‐
380 cific than the same physical pattern that is not associated with a vir‐
381 tual event. (e) given a sequence that matches two or more virtual
382 events, one of the virtual events will be chosen, but the order is
383 undefined.
384
385 If the matching sequences contain more than one event, then tests
386 (c)-(e) are applied in order from the most recent event to the least
387 recent event in the sequences. If these tests fail to determine a win‐
388 ner, then the most recently registered sequence is the winner.
389
390 If there are two (or more) virtual events that are both triggered by
391 the same sequence, and both of those virtual events are bound to the
392 same window tag, then only one of the virtual events will be triggered,
393 and it will be picked at random:
394
395 $widget->eventAdd('<<Paste>>' => '<Control-y>');
396 $widget->eventAdd('<<Paste>>' => '<Button-2>');
397 $widget->eventAdd <<Scroll>>' => '<Button-2>');
398 $widget->bind('Tk::Entry','<<Paste>>',sub { print 'Paste'});
399 $widget->bind('Tk::Entry','<<Scroll>>', sub {print 'Scroll'});
400
401 If the user types Control-y, the <<Paste>> binding will be invoked, but
402 if the user presses button 2 then one of either the <<Paste>> or the
403 <<Scroll>> bindings will be invoked, but exactly which one gets invoked
404 is undefined.
405
406 If an X event does not match any of the existing bindings, then the
407 event is ignored. An unbound event is not considered to be an error.
408
410 When a sequence specified in a bind method contains more than one event
411 pattern, then its callback is executed whenever the recent events
412 (leading up to and including the current event) match the given
413 sequence. This means, for example, that if button 1 is clicked repeat‐
414 edly the sequence <Double-ButtonPress-1> will match each button press
415 but the first. If extraneous events that would prevent a match occur
416 in the middle of an event sequence then the extraneous events are
417 ignored unless they are KeyPress or ButtonPress events. For example,
418 <Double-ButtonPress-1> will match a sequence of presses of button 1,
419 even though there will be ButtonRelease events (and possibly Motion
420 events) between the ButtonPress events. Furthermore, a KeyPress event
421 may be preceded by any number of other KeyPress events for modifier
422 keys without the modifier keys preventing a match. For example, the
423 event sequence aB will match a press of the a key, a release of the a
424 key, a press of the Shift key, and a press of the b key: the press of
425 Shift is ignored because it is a modifier key. Finally, if several
426 Motion events occur in a row, only the last one is used for purposes of
427 matching binding sequences.
428
430 If an error occurs in executing the callback for a binding then the
431 Tk::Error mechanism is used to report the error. The Tk::Error mecha‐
432 nism will be executed at same call level, and associated with the same
433 MainWindow as as the callback was invoked.
434
436 Note that for the Canvas widget, the call to bind has to be fully qual‐
437 ified. This is because there is already a bind method for the Canvas
438 widget, which binds individual canvas tags.
439
440 $canvas->Tk::bind
441
443 Tk::Error Tk::callbacks Tk::bindtags
444
446 Event, binding
447
448
449
450perl v5.8.8 2008-02-05 bind(3)