.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "bind 3" .TH bind 3 2023-07-25 "perl v5.38.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Tk::bind \- Arrange for X events to invoke callbacks .SH SYNOPSIS .IX Header "SYNOPSIS" Retrieve bindings: .PP \ \ \ \ \fR\f(CI$widget\fR\fI\fR\->\fBbind\fR .PP \ \ \ \ \fR\f(CI$widget\fR\fI\fR\->\fBbind\fR(\fItag\fR) .PP \ \ \ \ \fR\f(CI$widget\fR\fI\fR\->\fBbind\fR(\fIsequence\fR) .PP \ \ \ \ \fR\f(CI$widget\fR\fI\fR\->\fBbind\fR(\fItag\fR,\fIsequence\fR) .PP Associate and destroy bindings: .PP \ \ \ \ \fR\f(CI$widget\fR\fI\fR\->\fBbind\fR(\fIsequence\fR,\fIcallback\fR) .PP \ \ \ \ \fR\f(CI$widget\fR\fI\fR\->\fBbind\fR(\fItag\fR,\fIsequence\fR,\fIcallback\fR) .SH DESCRIPTION .IX Header "DESCRIPTION" The \fBbind\fR method associates callbacks with X events. If \fIcallback\fR is specified, \fBbind\fR will arrange for \fIcallback\fR to be evaluated whenever the event(s) given by \fIsequence\fR occur in the window(s) identified by \fR\f(CI$widget\fR\fI\fR or \fItag\fR. If \fIcallback\fR is an empty string then the current binding for \&\fIsequence\fR is destroyed, leaving \fIsequence\fR unbound. In all of the cases where a \fIcallback\fR argument is provided, \&\fBbind\fR returns an empty string. .PP If \fIsequence\fR is specified without a \fIcallback\fR, then the callback currently bound to \fIsequence\fR is returned, or \&\fBundef\fR is returned if there is no binding for \fIsequence\fR. If neither \fIsequence\fR nor \fIcallback\fR is specified, then the return value is a list whose elements are all the sequences for which there exist bindings for \fItag\fR. .PP If no \fItag\fR is specified then the \fBbind\fR refers to \fR\f(CI$widget\fR\fI\fR. If \fItag\fR is specified then it is typically a class name and the \fBbind\fR refers to all instances of the class on the \fBMainWindow\fR associated with \fI\fR\f(CI$widget\fR\fI\fR. (It is possible for \fItag\fR to be another "widget object" but this practice is deprecated.) Perl's \fBref\fR(\fI\fR\f(CI$object\fR\fI\fR) 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 \fBbindtags\fR method may be used to assign an arbitrary set of binding tags to a window, the default binding tags provide the following behavior: .PP If a tag is the name of an internal window the binding applies to that window. .PP If the tag is the name of a toplevel window the binding applies to the toplevel window and all its internal windows. .PP If the tag is the name of a class of widgets, such as \fBTk::Button\fR, the binding applies to all widgets in that class; .PP If \fItag\fR has the value \fBall\fR, the binding applies to all windows descended from the MainWindow of the application. .SH "EVENT PATTERNS" .IX Header "EVENT PATTERNS" The \fIsequence\fR argument specifies a sequence of one or more event patterns, with optional white space between the patterns. Each event pat may take one of three forms. In the simplest case it is a single printing ASCII character, such as \fBa\fR or \fB[\fR. The character may not be a space character or the character <. This form of pattern matches a \fBKeyPress\fR event for the particular character. The second form of pattern is longer but more general. It has the following syntax: .PP \ \ \ \ '' .PP 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 (\fIdetail\fR) identifying a particular button or keysym. Any of the fields may be omitted, as long as at least one of \fItype\fR and \fIdetail\fR is present. The fields must be separated by white space or dashes. .PP The third form of pattern is used to specify a user-defined, named virtual event; see Tk::event for details. It has the following syntax: .PP \ \ \ \ '<>' .PP The entire virtual event pattern is surrounded by double angle brackets. Inside the angle brackets is the user-defined name of the virtual event. Modifiers, such as \fBShift\fR or \fBControl\fR, may not be combined with a virtual event to modify it. Bindings on a virtual event may be created before the virtual event is defined, and if the definition of a virtual event changes dynamically, all windows bound to that virtual event will respond immediately to the new definition. .SH MODIFIERS .IX Header "MODIFIERS" Modifiers consist of any of the following values: .PP .Vb 9 \& 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, M1 Quadruple .Ve .PP Where more than one value is listed, separated by commas, the values are equivalent. Most of the modifiers have the obvious X meanings. For example, \fBButton1\fR 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 \fB\fR will match the event, but \fB\fR will not. If no modifiers are specified, then any combination of modifiers may be present in the event. .PP \&\fBMeta\fR and \fBM\fR refer to whichever of the \&\fBM1\fR through \fBM5\fR modifiers is associated with the meta key(s) on the keyboard (keysyms \fBMeta_R\fR and \fBMeta_L\fR). If there are no meta keys, or if they are not associated with any modifiers, then \fBMeta\fR and \fBM\fR will not match any events. Similarly, the \fBAlt\fR modifier refers to whichever modifier is associated with the alt key(s) on the keyboard (keysyms \&\fBAlt_L\fR and \fBAlt_R\fR). .PP The \fBDouble\fR, \fBTriple\fR and \fBQuadruple\fR modifiers are a convenience for specifying double mouse clicks and other repeated events. They cause a particular event pattern to be repeated 2, 3 or 4 times, and also place a time and space requirement on the sequence: for a sequence of events to match a \fBDouble\fR, \fBTriple\fR or \fBQuadruple\fR pattern, all of the events must occur close together in time and without substantial mouse motion in between. For example, \&\fB\fR is equivalent to \&\fB\fR with the extra time and space requirement. .SH "EVENT TYPES" .IX Header "EVENT TYPES" The \fItype\fR 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. .PP .Vb 10 \& Activate Destroy Map \& ButtonPress, Button Enter MapRequest \& ButtonRelease Expose Motion \& Circulate FocusIn MouseWheel \& CirculateRequest FocusOut Property \& Colormap Gravity Reparent \& Configure KeyPress, Key ResizeRequest \& ConfigureRequest KeyRelease Unmap \& Create Leave Visibility \& Deactivate .Ve .PP Most of the above events have the same fields and behaviors as events in the X Windowing system. You can find more detailed descriptions of these events in any X window programming book. A couple of the events are extensions to the X event system to support features unique to the Macintosh and Windows platforms. We provide a little more detail on these events here. These include: .PP .Vb 2 \& Activate \& Deactivate .Ve .PP These two events are sent to every sub-window of a toplevel when they change state. In addition to the focus Window, the Macintosh platform and Windows platforms have a notion of an active window (which often has but is not required to have the focus). On the Macintosh, widgets in the active window have a different appearance than widgets in deactive windows. The Activate event is sent to all the sub-windows in a toplevel when it changes from being deactive to active. Likewise, the Deactive event is sent when the window's state changes from active to deactive. There are no use\- ful percent substitutions you would make when binding to these events. .PP .Vb 1 \& MouseWheel .Ve .PP Some mice on the Windows platform support a mouse wheel which is used for scrolling documents without using the scrollbars. By rolling the wheel, the system will generate MouseWheel events that the application can use to scroll. Like Key events the event is always routed to the window that currently has focus. When the event is received you can use the \f(CW%D\fR substitution to get the delta field for the event which is a integer value of motion that the mouse wheel has moved. The smallest value for which the system will report is defined by the OS. On Windows 95 & 98 machines this value is at least 120 before it is reported. However, higher resolution devices may be available in the future. The sign of the value determines which direction your widget should scroll. Positive values should scroll up and negative values should scroll down. .PP The last part of a long event specification is \fIdetail\fR. In the case of a \fBButtonPress\fR or \fBButtonRelease\fR 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 \fItype\fR may be omitted: if will default to \fBButtonPress\fR. For example, the specifier \fB<1>\fR is equivalent to \fB\fR. .PP If the event type is \fBKeyPress\fR or \fBKeyRelease\fR, then \&\fIdetail\fR 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 \fB'K'\fR notation described below to print out the keysym name for a particular key. If a keysym \fIdetail\fR is given, then the \&\fItype\fR field may be omitted; it will default to \fBKeyPress\fR. For example, \fB\fR is equivalent to \&\fB\fR. .SH "BINDING CALLBACKS AND SUBSTITUTIONS" .IX Header "BINDING CALLBACKS AND SUBSTITUTIONS" The \fIcallback\fR argument to \fBbind\fR is a perl/Tk callback. which will be executed whenever the given event sequence occurs. (See Tk::callbacks for description of the possible forms.) \&\fICallback\fR will be associated with the same \fBMainWindow\fR that is associated with the \fR\f(CI$widget\fR\fI\fR that was used to invoke the \fBbind\fR method, and it will run as though called from \fBMainLoop\fR. If \fIcallback\fR contains any \fBEv\fR(\fI%\fR) calls, then each "nested" \fBEv\fR(\fI%\fR) "callback" will be evaluated when the event occurs to form arguments to be passed to the main \fIcallback\fR. The replacement depends on the character \fI%\fR, 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 \fBundef\fR!). .IP \fB'#'\fR 4 .IX Item "'#'" The number of the last client request processed by the server (the \fIserial\fR field from the event). Valid for all event types. .IP \fB'a'\fR 4 .IX Item "'a'" The \fIabove\fR field from the event, formatted as a hexadecimal number. Valid only for \fBConfigure\fR events. .IP \fB'b'\fR 4 .IX Item "'b'" The number of the button that was pressed or released. Valid only for \fBButtonPress\fR and \fBButtonRelease\fR events. .IP \fB'c'\fR 4 .IX Item "'c'" The \fIcount\fR field from the event. Valid only for \fBExpose\fR events. .IP \fB'd'\fR 4 .IX Item "'d'" The \fIdetail\fR field from the event. The \fB'd'\fR is replaced by a string identifying the detail. For \fBEnter\fR, \&\fBLeave\fR, \fBFocusIn\fR, and \fBFocusOut\fR events, the string will be one of the following: .Sp .Vb 4 \& NotifyAncestor NotifyNonlinearVirtual \& NotifyDetailNone NotifyPointer \& NotifyInferior NotifyPointerRoot \& NotifyNonlinear NotifyVirtual .Ve .Sp For \fBConfigureRequest\fR events, the string will be one of: .Sp .Vb 3 \& Above Opposite \& Below None \& BottomIf TopIf .Ve .Sp For events other than these, the substituted string is undefined. (Note that this is \fInot\fR the same as Detail part of sequence use to specify the event.) .IP \fB'f'\fR 4 .IX Item "'f'" The \fIfocus\fR field from the event (\fB0\fR or \fB1\fR). Valid only for \fBEnter\fR and \fBLeave\fR events. .IP \fB'h'\fR 4 .IX Item "'h'" The \fIheight\fR field from the event. Valid only for \fBConfigure\fR, \&\fBConfigureRequest\fR, \fBCreate\fR, \fBExpose\fR, and \fBResizeRequest\fR events. .IP \fB'i'\fR 4 .IX Item "'i'" The window field from the event, represented as a hexadecimal integer. .IP \fB'k'\fR 4 .IX Item "'k'" The \fIkeycode\fR field from the event. Valid only for \fBKeyPress\fR and \fBKeyRelease\fR events. .IP \fB'm'\fR 4 .IX Item "'m'" The \fImode\fR field from the event. The substituted string is one of \&\fBNotifyNormal\fR, \fBNotifyGrab\fR, \fBNotifyUngrab\fR, or \&\fBNotifyWhileGrabbed\fR. Valid only for \fBEnter\fR, \&\fBFocusIn\fR, \fBFocusOut\fR, and \fBLeave\fR events. .IP \fB'o'\fR 4 .IX Item "'o'" The \fIoverride_redirect\fR field from the event. Valid only for \&\fBMap\fR, \fBReparent\fR, and \fBConfigure\fR events. .IP \fB'p'\fR 4 .IX Item "'p'" The \fIplace\fR field from the event, substituted as one of the strings \fBPlaceOnTop\fR or \fBPlaceOnBottom\fR. Valid only for \fBCirculate\fR and \fBCirculateRequest\fR events. .IP \fB's'\fR 4 .IX Item "'s'" The \fIstate\fR field from the event. For \fBButtonPress\fR, \&\fBButtonRelease\fR, \fBEnter\fR, \fBKeyPress\fR, \fBKeyRelease\fR, \&\fBLeave\fR, and \fBMotion\fR events, a decimal string is substituted. For \fBVisibility\fR, one of the strings \&\fBVisibilityUnobscured\fR, \fBVisibilityPartiallyObscured\fR, and \fBVisibilityFullyObscured\fR is substituted. .IP \fB't'\fR 4 .IX Item "'t'" The \fItime\fR field from the event. Valid only for events that contain a \fItime\fR field. .IP \fB'w'\fR 4 .IX Item "'w'" The \fIwidth\fR field from the event. Valid only for \fBConfigure\fR, \&\fBConfigueRequest\fR, \fBCreate\fR, \fBExpose\fR, and \fBResizeREquest\fR events. .IP \fB'x'\fR 4 .IX Item "'x'" The \fIx\fR field from the event. Valid only for events containing an \fIx\fR field. .IP \fB'y'\fR 4 .IX Item "'y'" The \fIy\fR field from the event. Valid only for events containing a \fIy\fR field. .IP \fB'@'\fR 4 .IX Item "'@'" The string "@\fIx,y\fR" where \fIx\fR and \fIy\fR are as above. Valid only for events containing \fIx\fR and \fIy\fR fields. This format is used my methods of \fBTk::Text\fR and similar widgets. .IP \fB'A'\fR 4 .IX Item "'A'" Substitutes the UNICODE character corresponding to the event, or the empty string if the event doesn't correspond to a UNICODE character (e.g. the shift key was pressed). \fBXmbLookupString\fR does all the work of translating from the event to a UNICODE character. Valid only for \fBKeyPress\fR and \fBKeyRelease\fR events. .IP \fB'B'\fR 4 .IX Item "'B'" The \fIborder_width\fR field from the event. Valid only for \&\fBConfigure\fR, \fBConfigureRequest\fR and \fBCreate\fR events. .IP \fB'D'\fR 4 .IX Item "'D'" This reports the delta value of a \fBMouseWheel\fR event. The delta value represents the rotation units the mouse wheel has been moved. On Windows 95 & 98 systems the smallest value for the delta is 120. Future systems may support higher resolution values for the delta. The sign of the value represents the direction the mouse wheel was scrolled. .IP \fB'E'\fR 4 .IX Item "'E'" The \fIsend_event\fR field from the event. Valid for all event types. .IP \fB'K'\fR 4 .IX Item "'K'" The keysym corresponding to the event, substituted as a textual string. Valid only for \fBKeyPress\fR and \fBKeyRelease\fR events. .IP \fB'N'\fR 4 .IX Item "'N'" The keysym corresponding to the event, substituted as a decimal number. Valid only for \fBKeyPress\fR and \fBKeyRelease\fR events. .IP \fB'R'\fR 4 .IX Item "'R'" The \fIroot\fR window identifier from the event. Valid only for events containing a \fIroot\fR field. .IP \fB'S'\fR 4 .IX Item "'S'" The \fIsubwindow\fR window identifier from the event, as an object if it is one otherwise as a hexadecimal number. Valid only for events containing a \fIsubwindow\fR field. .IP \fB'T'\fR 4 .IX Item "'T'" The \fItype\fR field from the event. Valid for all event types. .IP \fB'W'\fR 4 .IX Item "'W'" The window to which the event was reported (the \&\f(CW$widget\fR field from the event) \- as an perl/Tk object. Valid for all event types. .IP \fB'X'\fR 4 .IX Item "'X'" The \fIx_root\fR field from the event. If a virtual-root window manager is being used then the substituted value is the corresponding x\-coordinate in the virtual root. Valid only for \&\fBButtonPress\fR, \fBButtonRelease\fR, \fBKeyPress\fR, \fBKeyRelease\fR, and \fBMotion\fR events. .IP \fB'Y'\fR 4 .IX Item "'Y'" The \fIy_root\fR field from the event. If a virtual-root window manager is being used then the substituted value is the corresponding y\-coordinate in the virtual root. Valid only for \&\fBButtonPress\fR, \fBButtonRelease\fR, \fBKeyPress\fR, \fBKeyRelease\fR, and \fBMotion\fR events. .SH "MULTIPLE MATCHES" .IX Header "MULTIPLE MATCHES" It is possible for several bindings to match a given X event. If the bindings are associated with different \fItag\fR's, then each of the bindings will be executed, in order. By default, a class binding will be executed first, followed by a binding for the widget, a binding for its toplevel, and an \fBall\fR binding. The \fBbindtags\fR method may be used to change this order for a particular window or to associate additional binding tags with the window. .PP \&\fBreturn\fR and \fBTk\->break\fR may be used inside a callback to control the processing of matching callbacks. If \fBreturn\fR is invoked, then the current callback is terminated but Tk will continue processing callbacks associated with other \fItag\fR's. If \fBTk\->break\fR is invoked within a callback, then that callback terminates and no other callbacks will be invoked for the event. (\fBTk\->break\fR is implemented via perl's \fBdie\fR with a special value which is "caught" by the perl/Tk "glue" code.) .PP If more than one binding matches a particular event and they have the same \fItag\fR, 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) an event pattern that specifies a specific button or key is more specific than one that doesn't; (b) a longer sequence (in terms of number of events matched) is more specific than a shorter sequence; (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. (d) a virtual event whose physical pattern matches the sequence is less specific than the same physical pattern that is not associated with a virtual event. (e) given a sequence that matches two or more virtual events, one of the virtual events will be chosen, but the order is undefined. .PP 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. .PP If there are two (or more) virtual events that are both triggered by the same sequence, and both of those virtual events are bound to the same window tag, then only one of the virtual events will be triggered, and it will be picked at random: .PP .Vb 5 \& $widget\->eventAdd(\*(Aq<>\*(Aq => \*(Aq\*(Aq); \& $widget\->eventAdd(\*(Aq<>\*(Aq => \*(Aq\*(Aq); \& $widget\->eventAdd <>\*(Aq => \*(Aq\*(Aq); \& $widget\->bind(\*(AqTk::Entry\*(Aq,\*(Aq<>\*(Aq,sub { print \*(AqPaste\*(Aq}); \& $widget\->bind(\*(AqTk::Entry\*(Aq,\*(Aq<>\*(Aq, sub {print \*(AqScroll\*(Aq}); .Ve .PP If the user types Control-y, the \fB<>\fR binding will be invoked, but if the user presses button 2 then one of either the \fB<>\fR or the \fB<>\fR bindings will be invoked, but exactly which one gets invoked is undefined. .PP 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. .SH "MULTI-EVENT SEQUENCES AND IGNORED EVENTS" .IX Header "MULTI-EVENT SEQUENCES AND IGNORED EVENTS" When a \fIsequence\fR specified in a \fBbind\fR 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 \fB\fR 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 \fBKeyPress\fR or \fBButtonPress\fR events. For example, \fB\fR will match a sequence of presses of button 1, even though there will be \fBButtonRelease\fR events (and possibly \fBMotion\fR events) between the \&\fBButtonPress\fR events. Furthermore, a \fBKeyPress\fR event may be preceded by any number of other \fBKeyPress\fR events for modifier keys without the modifier keys preventing a match. For example, the event sequence \fBaB\fR will match a press of the \&\fBa\fR key, a release of the \fBa\fR key, a press of the \fBShift\fR key, and a press of the \fBb\fR key: the press of \fBShift\fR is ignored because it is a modifier key. Finally, if several \fBMotion\fR events occur in a row, only the last one is used for purposes of matching binding sequences. .SH ERRORS .IX Header "ERRORS" If an error occurs in executing the callback for a binding then the \&\fBTk::Error\fR mechanism is used to report the error. The \fBTk::Error\fR mechanism will be executed at same call level, and associated with the same \fBMainWindow\fR as as the callback was invoked. .SH CAVEATS .IX Header "CAVEATS" Note that for the \fBCanvas\fR widget, the call to \fBbind\fR has to be fully qualified. This is because there is already a bind method for the \fBCanvas\fR widget, which binds individual canvas tags. .PP \ \ \ \ \fR\f(CI$canvas\fR\fI\fR\->\fBTk::bind\fR .SH "SEE ALSO" .IX Header "SEE ALSO" Tk::Error Tk::callbacks Tk::bindtags .SH KEYWORDS .IX Header "KEYWORDS" Event, binding