Debian Codegraph @

pod/bind.pod

raw
1 # Copyright (c) 1990 The Regents of the University of California.
2 # Copyright (c) 1994-1996 Sun Microsystems, Inc.
3 # See the file "license.terms" for information on usage and redistribution
4 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
5 #
6 #
7
8 =head1 NAME
9
10 Tk::bind - Arrange for X events to invoke callbacks
11
12 =for category Binding Events and Callbacks
13
14 =head1 SYNOPSIS
15
16 Retrieve bindings:
17
18 S< >I<$widget>-E<gt>B<bind>
19
20 S< >I<$widget>-E<gt>B<bind>(I<tag>)
21
22 S< >I<$widget>-E<gt>B<bind>(I<sequence>)
23
24 S< >I<$widget>-E<gt>B<bind>(I<tag>,I<sequence>)
25
26 Associate and destroy bindings:
27
28 S< >I<$widget>-E<gt>B<bind>(I<sequence>,I<callback>)
29
30 S< >I<$widget>-E<gt>B<bind>(I<tag>,I<sequence>,I<callback>)
31
32 =head1 DESCRIPTION
33
34 The B<bind> method associates callbacks with X events.
35 If I<callback> is specified, B<bind> will
36 arrange for I<callback> to be evaluated whenever
37 the event(s) given by I<sequence> occur in the window(s)
38 identified by I<$widget> or I<tag>.
39 If I<callback> is an empty string then the current binding for
40 I<sequence> is destroyed, leaving I<sequence> unbound.
41 In all of the cases where a I<callback> argument is provided,
42 B<bind> returns an empty string.
43
44 If I<sequence> is specified without a I<callback>, then the
45 callback currently bound to I<sequence> is returned, or
46 B<undef> is returned if there is no binding for I<sequence>.
47 If neither I<sequence> nor I<callback> is specified, then the
48 return value is a list whose elements are all the sequences
49 for which there exist bindings for I<tag>.
50
51 If no I<tag> is specified then the B<bind> refers to I<$widget>.
52 If I<tag> is specified then it is typically a class name and the B<bind>
53 refers to all instances of the class on the B<MainWindow> associated
54 with I<$widget>. (It is possible for I<tag> to be another "widget object"
55 but this practice is deprecated.) Perl's B<ref>(I<$object>) can be used
56 to get the class name of any object.
57 Each window has an associated list of tags, and a binding applies
58 to a particular window if its tag is among those specified for
59 the window.
60 Although the B<bindtags> method may be used to assign an
61 arbitrary set of binding tags to a window, the default binding
62 tags provide the following behavior:
63
64 If a tag is the name of an internal window the binding applies
65 to that window.
66
67 If the tag is the name of a toplevel window the binding applies
68 to the toplevel window and all its internal windows.
69
70 If the tag is the name of a class of widgets, such as B<Tk::Button>,
71 the binding applies to all widgets in that class;
72
73 If I<tag> has the value B<all>,
74 the binding applies to all windows descended from the MainWindow
75 of the application.
76
77 =head1 EVENT PATTERNS
78
79 The I<sequence> argument specifies a sequence of one or more event
80 patterns, with optional white space between the patterns. Each event
81 pat may take one of three forms. In the simplest case it is a single
82 printing ASCII character, such as B<a> or B<[>. The character may not
83 be a space character or the character <. This form of pattern matches
84 a B<KeyPress> event for the particular character. The second form of
85 pattern is longer but more general. It has the following syntax:
86
87 S< >'<modifier-modifier-type-detail>'
88
89 The entire event pattern is surrounded by angle brackets, and normally
90 needs to be quoted, as angle brackets are special to perl.
91 Inside the angle brackets are zero or more modifiers, an event
92 type, and an extra piece of information (I<detail>) identifying
93 a particular button or keysym. Any of the fields may be omitted,
94 as long as at least one of I<type> and I<detail> is present.
95 The fields must be separated by white space or dashes.
96
97 The third form of pattern is used to specify a user-defined, named virtual
98 event; see L<Tk::event> for details. It has the following syntax:
99
100 S< >'<<name>>'
101
102 The entire virtual event pattern is surrounded by double angle brackets.
103 Inside the angle brackets is the user-defined name of the virtual event.
104 Modifiers, such as B<Shift> or B<Control>, may not be combined with a
105 virtual event to modify it. Bindings on a virtual event may be created
106 before the virtual event is defined, and if the definition of a virtual
107 event changes dynamically, all windows bound to that virtual event will
108 respond immediately to the new definition.
109
110 =head1 MODIFIERS
111
112 Modifiers consist of any of the following values:
113
114 Control Mod2, M2
115 Shift Mod3, M3
116 Lock Mod4, M4
117 Button1, B1 Mod5, M5
118 Button2, B2 Meta, M
119 Button3, B3 Alt
120 Button4, B4 Double
121 Button5, B5 Triple
122 Mod1, M1 Quadruple
123
124 Where more than one value is listed, separated by commas, the values
125 are equivalent.
126 Most of the modifiers have the obvious X meanings.
127 For example, B<Button1> requires that
128 button 1 be depressed when the event occurs.
129 For a binding to match a given event, the modifiers in the event
130 must include all of those specified in the event pattern.
131 An event may also contain additional modifiers not specified in
132 the binding.
133 For example, if button 1 is pressed while the shift and control keys
134 are down, the pattern B<E<lt>Control-Button-1E<gt>> will match
135 the event, but B<E<lt>Mod1-Button-1E<gt>> will not.
136 If no modifiers are specified, then any combination of modifiers may
137 be present in the event.
138
139 B<Meta> and B<M> refer to whichever of the
140 B<M1> through B<M5> modifiers is associated with the meta
141 key(s) on the keyboard (keysyms B<Meta_R> and B<Meta_L>).
142 If there are no meta keys, or if they are not associated with any
143 modifiers, then B<Meta> and B<M> will not match any events.
144 Similarly, the B<Alt> modifier refers to whichever modifier
145 is associated with the alt key(s) on the keyboard (keysyms
146 B<Alt_L> and B<Alt_R>).
147
148 The B<Double>, B<Triple> and B<Quadruple> modifiers are a convenience
149 for specifying double mouse clicks and other repeated events. They
150 cause a particular event pattern to be repeated 2, 3 or 4 times, and
151 also place a time and space requirement on the sequence: for a
152 sequence of events to match a B<Double>, B<Triple> or B<Quadruple>
153 pattern, all of the events must occur close together in time and
154 without substantial mouse motion in between. For example,
155 B<E<lt>Double-Button-1E<gt>> is equivalent to
156 B<E<lt>Button-1E<gt>E<lt>Button-1E<gt>> with the extra time and space
157 requirement.
158
159 =head1 EVENT TYPES
160
161 The I<type> field may be any of the standard X event types, with a
162 few extra abbreviations. Below is a list of all the valid types;
163 where two names appear together, they are synonyms.
164
165
166 Activate Destroy Map
167 ButtonPress, Button Enter MapRequest
168 ButtonRelease Expose Motion
169 Circulate FocusIn MouseWheel
170 CirculateRequest FocusOut Property
171 Colormap Gravity Reparent
172 Configure KeyPress, Key ResizeRequest
173 ConfigureRequest KeyRelease Unmap
174 Create Leave Visibility
175 Deactivate
176
177 Most of the above events have the same fields and behaviors as events
178 in the X Windowing system. You can find more detailed descriptions of
179 these events in any X window programming book. A couple of the events
180 are extensions to the X event system to support features unique to the
181 Macintosh and Windows platforms. We provide a little more detail on
182 these events here. These include:
183
184 Activate
185 Deactivate
186
187 These two events are sent to every sub-window of a toplevel when they
188 change state. In addition to the focus Window, the Macintosh platform
189 and Windows platforms have a notion of an active window (which often
190 has but is not required to have the focus). On the Macintosh, widgets
191 in the active window have a different appearance than widgets in
192 deactive windows. The Activate event is sent to all the sub-windows
193 in a toplevel when it changes from being deactive to active.
194 Likewise, the Deactive event is sent when the window's state changes
195 from active to deactive. There are no use- ful percent substitutions
196 you would make when binding to these events.
197
198 MouseWheel
199
200 Some mice on the Windows platform support a mouse wheel which is
201 used for scrolling documents without using the scrollbars. By
202 rolling the wheel, the system will generate MouseWheel events that
203 the application can use to scroll. Like Key events the event is
204 always routed to the window that currently has focus. When the
205 event is received you can use the %D substitution to get the delta
206 field for the event which is a integer value of motion that the
207 mouse wheel has moved. The smallest value for which the system
208 will report is defined by the OS. On Windows 95 & 98 machines
209 this value is at least 120 before it is reported. However, higher
210 resolution devices may be available in the future. The sign of
211 the value determines which direction your widget should scroll.
212 Positive values should scroll up and negative values should scroll
213 down.
214
215 The last part of a long event specification is I<detail>. In the
216 case of a B<ButtonPress> or B<ButtonRelease> event, it is the
217 number of a button (1-5). If a button number is given, then only an
218 event on that particular button will match; if no button number is
219 given, then an event on any button will match. Note: giving a
220 specific button number is different than specifying a button modifier;
221 in the first case, it refers to a button being pressed or released,
222 while in the second it refers to some other button that is already
223 depressed when the matching event occurs. If a button
224 number is given then I<type> may be omitted: if will default
225 to B<ButtonPress>. For example, the specifier B<E<lt>1E<gt>>
226 is equivalent to B<E<lt>ButtonPress-1E<gt>>.
227
228 If the event type is B<KeyPress> or B<KeyRelease>, then
229 I<detail> may be specified in the form of an X keysym. Keysyms
230 are textual specifications for particular keys on the keyboard;
231 they include all the alphanumeric ASCII characters (e.g. ``a'' is
232 the keysym for the ASCII character ``a''), plus descriptions for
233 non-alphanumeric characters (``comma'' is the keysym for the comma
234 character), plus descriptions for all the non-ASCII keys on the
235 keyboard (``Shift_L'' is the keysm for the left shift key, and
236 ``F1'' is the keysym for the F1 function key, if it exists). The
237 complete list of keysyms is not presented here; it is
238 available in other X documentation and may vary from system to
239 system.
240 If necessary, you can use the B<'K'> notation described below
241 to print out the keysym name for a particular key.
242 If a keysym I<detail> is given, then the
243 I<type> field may be omitted; it will default to B<KeyPress>.
244 For example, B<E<lt>Control-commaE<gt>> is equivalent to
245 B<E<lt>Control-KeyPress-commaE<gt>>.
246
247 =head1 BINDING CALLBACKS AND SUBSTITUTIONS
248
249 The I<callback> argument to B<bind> is a perl/Tk callback.
250 which will be executed whenever the given event sequence occurs.
251 (See L<Tk::callbacks> for description of the possible forms.)
252 I<Callback> will be associated with the same B<MainWindow>
253 that is associated with the I<$widget> that was used to invoke
254 the B<bind> method, and it will run as though called from B<MainLoop>.
255 If I<callback> contains
256 any B<Ev>(I<%>) calls, then each "nested" B<Ev>(I<%>)
257 "callback" will be evaluated when the event occurs to form arguments
258 to be passed to the main I<callback>.
259 The replacement
260 depends on the character I<%>, as defined in the
261 list below. Unless otherwise indicated, the
262 replacement string is the numeric (decimal) value of the given field from
263 the current event. Perl/Tk has enhanced this mechanism slightly compared
264 to the comparable Tcl/Tk mechanism. The enhancements are not yet all
265 reflected in the list below.
266 Some of the substitutions are only valid for
267 certain types of events; if they are used for other types of events
268 the value substituted is undefined (not the same as B<undef>!).
269
270 =over 4
271
272 =item B<'#'>
273
274 The number of the last client request processed by the server
275 (the I<serial> field from the event). Valid for all event
276 types.
277
278 =item B<'a'>
279
280 The I<above> field from the event,
281 formatted as a hexadecimal number.
282 Valid only for B<Configure> events.
283
284 =item B<'b'>
285
286 The number of the button that was pressed or released. Valid only
287 for B<ButtonPress> and B<ButtonRelease> events.
288
289 =item B<'c'>
290
291 The I<count> field from the event. Valid only for B<Expose> events.
292
293 =item B<'d'>
294
295 The I<detail> field from the event. The B<'d'> is replaced by
296 a string identifying the detail. For B<Enter>,
297 B<Leave>, B<FocusIn>, and B<FocusOut> events,
298 the string will be one of the following:
299
300 NotifyAncestor NotifyNonlinearVirtual
301 NotifyDetailNone NotifyPointer
302 NotifyInferior NotifyPointerRoot
303 NotifyNonlinear NotifyVirtual
304
305 For B<ConfigureRequest> events, the string will be one of:
306
307 Above Opposite
308 Below None
309 BottomIf TopIf
310
311 For events other than these, the substituted string is undefined.
312 (Note that this is I<not> the same as Detail part of sequence
313 use to specify the event.)
314
315 =item B<'f'>
316
317 The I<focus> field from the event (B<0> or B<1>). Valid only
318 for B<Enter> and B<Leave> events.
319
320 =item B<'h'>
321
322 The I<height> field from the event. Valid only for B<Configure>,
323 B<ConfigureRequest>, B<Create>, B<Expose>, and B<ResizeRequest> events.
324
325 =item B<'i'>
326
327 The window field from the event, represented as a hexadecimal integer.
328
329 =item B<'k'>
330
331 The I<keycode> field from the event. Valid only for B<KeyPress>
332 and B<KeyRelease> events.
333
334 =item B<'m'>
335
336 The I<mode> field from the event. The substituted string is one of
337 B<NotifyNormal>, B<NotifyGrab>, B<NotifyUngrab>, or
338 B<NotifyWhileGrabbed>. Valid only for B<Enter>,
339 B<FocusIn>, B<FocusOut>, and B<Leave> events.
340
341 =item B<'o'>
342
343 The I<override_redirect> field from the event. Valid only for
344 B<Map>, B<Reparent>, and B<Configure> events.
345
346 =item B<'p'>
347
348 The I<place> field from the event, substituted as one of the
349 strings B<PlaceOnTop> or B<PlaceOnBottom>. Valid only
350 for B<Circulate> and B<CirculateRequest> events.
351
352 =item B<'s'>
353
354 The I<state> field from the event. For B<ButtonPress>,
355 B<ButtonRelease>, B<Enter>, B<KeyPress>, B<KeyRelease>,
356 B<Leave>, and B<Motion> events, a decimal string
357 is substituted. For B<Visibility>, one of the strings
358 B<VisibilityUnobscured>, B<VisibilityPartiallyObscured>,
359 and B<VisibilityFullyObscured> is substituted.
360
361 =item B<'t'>
362
363 The I<time> field from the event. Valid only for events that
364 contain a I<time> field.
365
366 =item B<'w'>
367
368 The I<width> field from the event. Valid only for B<Configure>,
369 B<ConfigueRequest>, B<Create>, B<Expose>, and B<ResizeREquest> events.
370
371 =item B<'x'>
372
373 The I<x> field from the event. Valid only for events containing
374 an I<x> field.
375
376 =item B<'y'>
377
378 The I<y> field from the event. Valid only for events containing
379 a I<y> field.
380
381 =item B<'@'>
382
383 The string "@I<x,y>" where I<x> and I<y> are as above.
384 Valid only for events containing I<x> and I<y> fields.
385 This format is used my methods of B<Tk::Text> and similar widgets.
386
387 =item B<'A'>
388
389 Substitutes the UNICODE character corresponding to the event, or
390 the empty string if the event doesn't correspond to a UNICODE character
391 (e.g. the shift key was pressed). B<XmbLookupString> does all the
392 work of translating from the event to a UNICODE character.
393 Valid only for B<KeyPress> and B<KeyRelease> events.
394
395 =item B<'B'>
396
397 The I<border_width> field from the event. Valid only for
398 B<Configure>, B<ConfigureRequest> and B<Create> events.
399
400 =item B<'D'>
401
402 This reports the delta value of a B<MouseWheel> event. The delta
403 value represents the rotation units the mouse wheel has been
404 moved. On Windows 95 & 98 systems the smallest value for the
405 delta is 120. Future systems may support higher resolution values
406 for the delta. The sign of the value represents the direction the
407 mouse wheel was scrolled.
408
409 =item B<'E'>
410
411 The I<send_event> field from the event. Valid for all event types.
412
413 =item B<'K'>
414
415 The keysym corresponding to the event, substituted as a textual
416 string. Valid only for B<KeyPress> and B<KeyRelease> events.
417
418 =item B<'N'>
419
420 The keysym corresponding to the event, substituted as
421 a decimal
422 number. Valid only for B<KeyPress> and B<KeyRelease> events.
423
424 =item B<'R'>
425
426 The I<root> window identifier from the event. Valid only for
427 events containing a I<root> field.
428
429 =item B<'S'>
430
431 The I<subwindow> window identifier from the event,
432 as an object if it is one otherwise as a hexadecimal number.
433 Valid only for events containing a I<subwindow> field.
434
435 =item B<'T'>
436
437 The I<type> field from the event. Valid for all event types.
438
439 =item B<'W'>
440
441 The window to which the event was reported (the
442 $widget field from the event) - as an perl/Tk object.
443 Valid for all event types.
444
445 =item B<'X'>
446
447 The I<x_root> field from the event.
448 If a virtual-root window manager is being used then the substituted
449 value is the corresponding x-coordinate in the virtual root.
450 Valid only for
451 B<ButtonPress>, B<ButtonRelease>, B<KeyPress>, B<KeyRelease>,
452 and B<Motion> events.
453
454 =item B<'Y'>
455
456 The I<y_root> field from the event.
457 If a virtual-root window manager is being used then the substituted
458 value is the corresponding y-coordinate in the virtual root.
459 Valid only for
460 B<ButtonPress>, B<ButtonRelease>, B<KeyPress>, B<KeyRelease>,
461 and B<Motion> events.
462
463 =back
464
465 =head1 MULTIPLE MATCHES
466
467 It is possible for several bindings to match a given X event.
468 If the bindings are associated with different I<tag>'s,
469 then each of the bindings will be executed, in order.
470 By default, a class binding will be executed first, followed
471 by a binding for the widget, a binding for its toplevel, and
472 an B<all> binding.
473 The B<bindtags> method may be used to change this order for
474 a particular window or to associate additional binding tags with
475 the window.
476
477 B<return> and B<Tk-E<gt>break> may be used inside a
478 callback to control the processing of matching callbacks.
479 If B<return> is invoked, then the current callback
480 is terminated but Tk will continue processing callbacks
481 associated with other I<tag>'s.
482 If B<Tk-E<gt>break> is invoked within a callback,
483 then that callback terminates and no other callbacks will be invoked
484 for the event.
485 (B<Tk-E<gt>break> is implemented via perl's B<die> with a special value
486 which is "caught" by the perl/Tk "glue" code.)
487
488 If more than one binding matches a particular event and they
489 have the same I<tag>, then the most specific binding
490 is chosen and its callback is evaluated.
491 The following tests are applied, in order, to determine which of
492 several matching sequences is more specific:
493 (a) an event pattern that specifies a specific button or key is more specific
494 than one that doesn't;
495 (b) a longer sequence (in terms of number
496 of events matched) is more specific than a shorter sequence;
497 (c) if the modifiers specified in one pattern are a subset of the
498 modifiers in another pattern, then the pattern with more modifiers
499 is more specific.
500 (d) a virtual event whose physical pattern matches the sequence is less
501 specific than the same physical pattern that is not associated with a
502 virtual event.
503 (e) given a sequence that matches two or more virtual events, one
504 of the virtual events will be chosen, but the order is undefined.
505
506 If the matching sequences contain more than one event, then tests
507 (c)-(e) are applied in order from the most recent event to the least recent
508 event in the sequences. If these tests fail to determine a winner, then the
509 most recently registered sequence is the winner.
510
511 If there are two (or more) virtual events that are both triggered by the
512 same sequence, and both of those virtual events are bound to the same window
513 tag, then only one of the virtual events will be triggered, and it will
514 be picked at random:
515
516 $widget->eventAdd('<<Paste>>' => '<Control-y>');
517 $widget->eventAdd('<<Paste>>' => '<Button-2>');
518 $widget->eventAdd <<Scroll>>' => '<Button-2>');
519 $widget->bind('Tk::Entry','<<Paste>>',sub { print 'Paste'});
520 $widget->bind('Tk::Entry','<<Scroll>>', sub {print 'Scroll'});
521
522 If the user types Control-y, the B<E<lt>E<lt>PasteE<gt>E<gt>> binding
523 will be invoked, but if the user presses button 2 then one of
524 either the B<E<lt>E<lt>PasteE<gt>E<gt>> or the B<E<lt>E<lt>ScrollE<gt>E<gt>> bindings will
525 be invoked, but exactly which one gets invoked is undefined.
526
527 If an X event does not match any of the existing bindings, then the
528 event is ignored.
529 An unbound event is not considered to be an error.
530
531 =head1 MULTI-EVENT SEQUENCES AND IGNORED EVENTS
532
533 When a I<sequence> specified in a B<bind> method contains
534 more than one event pattern, then its callback is executed whenever
535 the recent events (leading up to and including the current event)
536 match the given sequence. This means, for example, that if button 1 is
537 clicked repeatedly the sequence B<E<lt>Double-ButtonPress-1E<gt>> will match
538 each button press but the first.
539 If extraneous events that would prevent a match occur in the middle
540 of an event sequence then the extraneous events are
541 ignored unless they are B<KeyPress> or B<ButtonPress> events.
542 For example, B<E<lt>Double-ButtonPress-1E<gt>> will match a sequence of
543 presses of button 1, even though there will be B<ButtonRelease>
544 events (and possibly B<Motion> events) between the
545 B<ButtonPress> events.
546 Furthermore, a B<KeyPress> event may be preceded by any number
547 of other B<KeyPress> events for modifier keys without the
548 modifier keys preventing a match.
549 For example, the event sequence B<aB> will match a press of the
550 B<a> key, a release of the B<a> key, a press of the B<Shift>
551 key, and a press of the B<b> key: the press of B<Shift> is
552 ignored because it is a modifier key.
553 Finally, if several B<Motion> events occur in a row, only
554 the last one is used for purposes of matching binding sequences.
555
556 =head1 ERRORS
557
558 If an error occurs in executing the callback for a binding then the
559 B<Tk::Error> mechanism is used to report the error.
560 The B<Tk::Error> mechanism will be executed at same call level,
561 and associated with the same B<MainWindow> as
562 as the callback was invoked.
563
564 =head1 CAVEATS
565
566 Note that for the B<Canvas> widget, the call to B<bind> has to be
567 fully qualified. This is because there is already a bind method for
568 the B<Canvas> widget, which binds individual canvas tags.
569
570 S< >I<$canvas>-E<gt>B<Tk::bind>
571
572 =head1 SEE ALSO
573
574 L<Tk::Error|Tk::Error>
575 L<Tk::callbacks|Tk::callbacks>
576 L<Tk::bindtags|Tk::bindtags>
577
578 =head1 KEYWORDS
579
580 Event, binding
581
582 =cut
583