The bind method associates callbacks with X events. If callback is specified, bind will arrange for callback to be evaluated whenever the event(s) given by sequence occur in the window(s) identified by $widget or tag. If callback is an empty string then the current binding for sequence is destroyed, leaving sequence unbound. In all of the cases where a callback argument is provided, bind returns an empty string.
If sequence is specified without a callback, then the callback currently bound to sequence is returned, or an empty string is returned if there is no binding for sequence. If neither sequence nor callback is specified, then the return value is a list whose elements are all the sequences for which there exist bindings for tag.
If no tag is specified then the bind refers to $widget. If tag is specified then it is typically a class name and the bind refers to all instances of the class on the MainWindow associated with $widget. (It is possible for tag to be another "widget object" but this practice is deprecated.) Perl's ref($object) can be used to get the class name of any object. Each window has an associated list of tags, and a binding applies to a particular window if its tag is among those specified for the window. Although the bindtags method may be used to assign an arbitrary set of binding tags to a window, the default binding tags provide the following behavior:
If a tag is the name of an internal window the binding applies to that window.
If the tag is the name of a toplevel window the binding applies to the toplevel window and all its internal windows.
If the tag is the name of a class of widgets, such as Tk::Button, the binding applies to all widgets in that class;
If tag has the value all, the binding applies to all windows in the application.
The sequence argument specifies a sequence of one or more
event patterns, with optional white space between the patterns. Each
event pattern has the following syntax:
'<modifier-modifier-type-detail>'The entire event pattern is surrounded by angle brackets, and normally needs to be quoted, as angle brackets are special to perl. Inside the angle brackets are zero or more modifiers, an event type, and an extra piece of information (detail) identifying a particular button or keysym. Any of the fields may be omitted, as long as at least one of type and detail is present. The fields must be separated by white space or dashes.
Modifiers consist of any of the following values:
Control Mod2, M2 Shift Mod3, M3 Lock Mod4, M4 Button1, B1 Mod5, M5 Button2, B2 Meta, M Button3, B3 Alt Button4, B4 Double Button5, B5 Triple Mod1, M1Where more than one value is listed, separated by commas, the values are equivalent. Most of the modifiers have the obvious X meanings. For example, Button1 requires that button 1 be depressed when the event occurs. For a binding to match a given event, the modifiers in the event must include all of those specified in the event pattern. An event may also contain additional modifiers not specified in the binding. For example, if button 1 is pressed while the shift and control keys are down, the pattern <Control-Button-1> will match the event, but <Mod1-Button-1> will not. If no modifiers are specified, then any combination of modifiers may be present in the event.
Meta and M refer to whichever of the M1 through M5 modifiers is associated with the meta key(s) on the keyboard (keysyms Meta_R and Meta_L). If there are no meta keys, or if they are not associated with any modifiers, then Meta and M will not match any events. Similarly, the Alt modifier refers to whichever modifier is associated with the alt key(s) on the keyboard (keysyms Alt_L and Alt_R).
The Double and Triple modifiers are a convenience for specifying double mouse clicks and other repeated events. They cause a particular event pattern to be repeated 2 or 3 times, and also place a time and space requirement on the sequence: for a sequence of events to match a Double or Triple pattern, all of the events must occur close together in time and without substantial mouse motion in between. For example, <Double-Button-1> is equivalent to <Button-1><Button-1> with the extra time and space requirement.
The type field may be any of the standard X event types, with a
few extra abbreviations. Below is a list of all the valid types;
where two names appear together, they are synonyms.
ButtonPress, Button Expose Map ButtonRelease FocusIn Motion Circulate FocusOut Property Colormap Gravity Reparent Configure KeyPress, Key Unmap Destroy KeyRelease Visibility Enter Leave
The last part of a long event specification is detail. In the case of a ButtonPress or ButtonRelease event, it is the number of a button (1-5). If a button number is given, then only an event on that particular button will match; if no button number is given, then an event on any button will match. Note: giving a specific button number is different than specifying a button modifier; in the first case, it refers to a button being pressed or released, while in the second it refers to some other button that is already depressed when the matching event occurs. If a button number is given then type may be omitted: if will default to ButtonPress. For example, the specifier <1> is equivalent to <ButtonPress-1>.
If the event type is KeyPress or KeyRelease, then detail may be specified in the form of an X keysym. Keysyms are textual specifications for particular keys on the keyboard; they include all the alphanumeric ASCII characters (e.g. ``a'' is the keysym for the ASCII character ``a''), plus descriptions for non-alphanumeric characters (``comma'' is the keysym for the comma character), plus descriptions for all the non-ASCII keys on the keyboard (``Shift_L'' is the keysm for the left shift key, and ``F1'' is the keysym for the F1 function key, if it exists). The complete list of keysyms is not presented here; it is available in other X documentation and may vary from system to system. If necessary, you can use the 'K' notation described below to print out the keysym name for a particular key. If a keysym detail is given, then the type field may be omitted; it will default to KeyPress. For example, <Control-comma> is equivalent to <Control-KeyPress-comma>.
The callback argument to bind is a perl/Tk callback. which will be executed whenever the given event sequence occurs. (See callbacks for description of the possible forms.) Callback will be associated with the same MainWindow that is associated with the $widget that was used to invoke the bind method, and it will run as though called from MainLoop - typically only global variables should be assumed accessible. If callback contains any Ev(%) calls, then each "nested" Ev(%) "callback" will be evaluated when the event occurs to form arguments to be passed to the main callback. The replacement depends on the character %, as defined in the list below. Unless otherwise indicated, the replacement string is the numeric (decimal) value of the given field from the current event. Perl/Tk has enhanced this mechanism slightly compared to the comparable Tcl/Tk mechanism. The enhancements are not yet all reflected in the list below. Some of the substitutions are only valid for certain types of events; if they are used for other types of events the value substituted is undefined (not the same as undef!).
NotifyAncestor NotifyNonlinearVirtual NotifyDetailNone NotifyPointer NotifyInferior NotifyPointerRoot NotifyNonlinear NotifyVirtualFor events other than these, the substituted string is undefined.
return and Tk->break may be used inside a callback to control the processing of matching callbacks. If return is invoked, then the current callback is terminated but Tk will continue processing callbacks associated with other tag's. If Tk->break is invoked within a callback, then that callback terminates and no other callbacks will be invoked for the event. (Tk->break is implemented via perl's die with a special value which is "caught" by the perl/Tk "glue" code.)
If more than one binding matches a particular event and they have the same tag, then the most specific binding is chosen and its callback is evaluated. The following tests are applied, in order, to determine which of several matching sequences is more specific: (a) a longer sequence (in terms of number of events matched) is more specific than a shorter sequence; (b) an event pattern that specifies a specific button or key is more specific than one that doesn't; (c) 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. If the matching sequences contain more than one event, then tests (c)-(e) 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 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.
When a sequence specified in a bind method contains more than one event pattern, then its callback is executed whenever the recent events (leading up to and including the current event) match the given sequence. This means, for example, that if button 1 is clicked repeatedly the sequence <Double-ButtonPress-1> will match each button press but the first. If extraneous events that would prevent a match occur in the middle of an event sequence then the extraneous events are ignored unless they are KeyPress or ButtonPress events. For example, <Double-ButtonPress-1> will match a sequence of presses of button 1, even though there will be ButtonRelease events (and possibly Motion events) between the ButtonPress events. Furthermore, a KeyPress event may be preceded by any number of other KeyPress events for modifier keys without the modifier keys preventing a match. For example, the event sequence aB will match a press of the a key, a release of the a key, a press of the Shift key, and a press of the b key: the press of Shift is ignored because it is a modifier key. Finally, if several Motion events occur in a row, only the last one is used for purposes of matching binding sequences.
If an error occurs in executing the callback for a binding then the Tk::Error mechanism is used to report the error. The Tk::Error mechanism will be executed at same call level, and associated with the same MainWindow as as the callback was invoked.