bind - Arrange for X events to invoke Tcl scripts '''bind''' ''tag'' : '''bind''' ''tag sequence'' '''bind''' ''tag sequence'' : '''bind''' ''tag sequence script'' '''bind''' ''tag sequence script'' : '''bind''' ''tag sequence '''''+'''''script'' '''bind''' ''tag sequence '''''+'''''script'' http://www.purl.org/tcl/home/man/tcl8.4/TkCmd/bind.htm describes the Tk bind command. This command is used to associate Tcl commands to execute upon the press of specific keys. ** Description ** Also, bind may be driven by events, both real and virtual, such as enter, leave, focus in, focus out, etc. See the documentation for specific details. `bind` (part of [Tk]) is used to associate Tcl commands to execute upon See http://www.purl.org/tcl/home/man/tcl8.4/TkCmd/keysyms.htm for the keyboard symbols to which one can potentially bind things. ''tag'' is formally a `[bindtags]` tag, but for most user code it is best to Keyboard symbols are the Tk "strings" associated with particular sequences of keyboard keys that one can press, such as Shift, or A or special function keys such as Page Up, etc. Of course, the specific keysyms available to you on any particular platform/hardware depends on factors outside of Tk's control. [Bindings and why they are important] [Bindings and variable substitution] ----- If you are looking for the [eggdrop] bind command, try this: [http://www.eggheads.org/support/egghtml/1.6.17/tcl-commands.html#bind] <> ----- Question: Is there an introspective method that a Tk application can determine whether a keysym is currently available for it to use? ---- [RS] writes on comp.lang.tcl: The bind syntax is actually very easy. With x set to a widget (e.g. .mytext) or a class (e.g. Text), call bind $x to get a list of defined bindings. This list contains which is the to get a list of defined bindings. This list contains which is the correct name for the Page Up key. To see what it is bound to, call ====== bind $x To mirror this binding to another event, make that ====== bind $x [bind $x ] like you tried, just with the incorrect name. BTW, you normally need not like you tried, just with the incorrect name. BTW, you normally need not include variable names in braces - only if they contain characters other than A-Z a-z 0-9 _. ---- [KBK] - You can find the keysym for a key on an unfamiliary keyboard by running ''wish'' against the one-line script: ** Finding keysyms *** bind . { puts %K } ; focus . directing focus at the (empty) ''wish'' window, and touching the key in directing focus at the (empty) ''wish'' window, and touching the key in question. (On Windows, you'll need to add ====== console show to the script so that you can see its output. to the script so that you can see its output!) [Arjen Markus]: You may also put up a text widget and display the keysym by the [Arjen Markus] You may also put up a text widget and display the keysym by the following script: ====== bind .textwidget { .textwidget insert end "%K " } ; focus .textwidget ---- ** Specifying a Key to `bind` ** [Arjen Markus] There are several ways to define a specific key, for instance: ====== (subtle differences here!) (if the character is plain ASCII) For a space, you will need to use: ====== anything else seems to give problems. Also note that keysyms are case-sensitive: "Down" for the downward arrow and Also note that keysyms are case-sensitive: "Down" for the downward arrow and "space" for a space ("down" and "Space" do ''not'' work; quotation marks for convenience only) ---- [LV] If you are wanting to bind actions to a mouse button action, rather than a keyboard press, then you appear to be able to use: ** `bind` to a Mouse Action ** bind $x { puts "Unmodified button" } bind $x { puts "Shift button 1 release" } [Martin Lemburg] July 2nd, 2002: [Martin Lemburg] 2002-07-02: Why isn't there an event ''Invoke'' for invokeable I have a question: Than ... Why isn't there an event ''Invoke'' for invokeable widgets, like buttons? Wouldn't make it sense to have such an event? To bind widgets using the ''Invoke'' event, like to connect a widget with the ''-command'' option to an event handler? ====== button .b -text exit -command {cmdProc .b [clock seconds]}; ... would be equal to ... ====== bind .b {cmdProc %W %t}; It shouldn't be a problem to use the substitution capabilities during the usage of a binding, like: ====== button .b -text exit -command {cmdProc %W %t}; Wouldn't that be nice and consequent? Wouldn't it be consequent (for example) to use bindings to scrollbars or Wouldn't it be consequent (for example) to use bindings to scrollbars or entries too? ... ====== entry .e -textvariable filter; button .b -text "filter"; listbox .lb -listvar data -selectmode extended; scrollbar .sbx -orient horizontal; scrollbar .sby -orient vertical; bind .e {validateFilter %V %s}; bind .e {validateFilter %V %s}; bind .b {filter .e .lb}; bind .lb {.sbx set}; bind .lb {.sby set}; bind .sbx {.lb xview}; bind .sby {.lb yview}; There would be the chance to elimate all (event)handlers from that code, that There would be the chance to elimate all (event)handlers from that code, that only builds up the GUI. The code to handle events could use now use bindings. ---- [Ken Jones] writes on comp.lang.tcl, in response to a developer trying to bind actions on numeric keys using ** Caution: Abbreviated `bind` Descriptions ** bind .bu1 "focus .bu2" What you're experiencing is one of those traps that people encounter when they the following response: On the other hand, Tcl also allows you to abbreviate ButtonPress events, so <1> What you're experiencing is one of those traps that people encounter when they use abbreviated binding descriptions. For many KeyPress events, you can get by with providing only the keysym. So, is equivalent to , and is equivalent to . Solution? `bind` to `` and ``. And be very careful On the other hand, Tcl also allows you to abbreviate ButtonPress events, so <1> is equivalent to . And through is equivalent to through . Solution? Bind to and . And be very careful relying on abbreviated event descriptions in bindings. ---- [LV] I am trying to get the following sample program to display the numbers ====== package require Tk #! /usr/tcl84/bin/tclsh entry .e package require Tk The expectation was that I would get an entry widget into which I could press entry .e bind .e { puts 1 } pack .e focus .e The expectation was that I would get an entry widget into which I could press the keys on the keypad and get , in this case, the number 1 into the entry widget. What I experience is that after I mouse into the widget and click, I can type alphas and the numbers on top row of the main keyboard, but the keypad keys are generating nothing... Peter Newman 2004-04-30: ''NumLock'' switches the keypad between numbers and Peter Newman 30 April 2004: ''NumLock'' switches the keypad between numbers and cursor keys. I presume you tried both settings - and still no joy. If so, then it would appear that Tk doesn't support the keyboard properly. Pierre Coueffin 2004-04-12: Try `` Pierre Coueffin 12 April 2005: Try Thomas Guettler 26 April 2006: Or Try `` Thomas Guettler 26 April 2006: Or Try ====== ---- Q. Given a string specifying an event sequence, how to obtain some sort of "normal form" for that event sequence, so that, for example, '1' and 'Mousebutton-1' (or whatever the long form is) both map to the same normal form indicating a single click of mouse button 1? Q. Given a string specifying an event sequence, how to obtain some sort of A. ([KBK]) Hmm, bind it to a nonexistent bindtag and then query the bindings ======none % bind Nothing {;} % bind Nothing ---- Find out on [mousewheel] bindings. ---- ** Control Sequences ** [Dossy] 29mar2005: I just spent a good hour only trying to understand [[bind]] only to discover that [[bind . {script}]] is NOT the same as [[bind . {script}]] -- the former works, the latter doesn't. [RS] would expect that is equivalent to ... case matters, doesn't it? [Dossy] 2005-03-29: I just spent a good hour only trying to understand `bind` [MG] The bindings for this, after a little playing, seem slightly strange. Without Caps Lock on, Shift and the 'a' key (to get an upper-case A) fires the binding for Shift and (uppercase) A. The only way to fire a binding for Shift and (lowercase) a that I can find is to turn Caps Lock on, and -then- press the 'a' key with Shift held down. [RS]: would expect that `` is equivalent to `` ... case CJL The minimal example of this mildly unexpected behaviour is to switch Caps Lock on, then in a Wish console (I'm talking Windoze here) type something, highlight it and press Ctrl-C. Now try to paste your text somewhere else (e.g. Notepad) using Ctrl-V - you'll discover that the copy didn't happen, but even though Caps Lock is still on, Notepad will quite happily obey the paste request (by pasting whatever was already in the clipboard). To me 'Ctrl-C' means "the physical key labelled with a 'C' was pressed while ctrl was down", and should have nothing to do with the state of Caps Lock, only the combination of keys pressed. [MG]: The bindings for this, after a little playing, seem slightly strange. [WHD]: On both Windows and Mac OS X, normal apps accept both Control-v and Control-V (Command-v and Command-V) as the Paste key. For Tk, it appears that you need to bind both. On the other hand, if you bind both you'll find that all of the following combinations will paste: CJL The minimal example of this mildly unexpected behaviour is to switch Caps * Control-v * Control-V * Shift-Control-v * Shift-Control-V If you want to support Shift-Control sequences in your keyset, you need to bind If you want to support Shift-Control sequences in your keyset, you need to bind both Shift-Control-v and Shift-Control-V. Here's a simple solution that results in CapsLock-independent letter key bindings: bind $tag <$modifier-$upper> $binding proc bindletterkey {tag modifier letter binding} { set upper [string toupper $letter] set lower [string tolower $letter] bindletterkey .text Control F {puts Control-F} bind $tag <$modifier-$upper> $binding bind $tag <$modifier-$lower> $binding } bindletterkey .text Control F {puts "Control-F"} bindletterkey .text Shift-Control F {puts "Shift-Control-F"} ** Case Insensitive Binding ** [MG] 2005-06-06: And, where the bindings already exist (ie, for the Text [MG] adds, on June 6 2005 - And, where the bindings already exist (ie, for the Text widget) and you want to make them case-insensitive, something like this will help. ====== proc mirror {class {dir 1}} { if { $dir == "1" } { set range "a-z" set case "toupper" } else { set range "A-Z" set case "tolower" } foreach x [bind $class] { if { [regexp "^<(.+-)?[$range]>$" $x] } { set y [string range $x end-1 end-1] set y "[string range $x 0 end-2][string $case $y]>" if { [bind $class $y] != "" } { continue; } bind $class $y [bind $class $x] } } };# mirror Then run, for instance: ====== mirror Text 1; # copy all lower-class bindings for the Text widget to upper-class versions mirror Text 0; # copy all upper-class bindings for the Text widget to lower-class versions mirror Entry 1; # copy all lower-class bindings for the Entry widget to upper-class versions It's also careful not to overwrite bindings - if you have an and an It's also careful not to overwrite bindings - if you have an and an binding for the Text widget, and run ''mirror Text 1'', will be left as it is, rather than being overwritten with 's binding. This raises one question for me, though - what's better practice? Should you This raises one question for me, though - what's better practice? Should you bind to bind Text [bind Text ] bind Text {event generate %W } ---- [LV] See also [binding to a single mouse click] ** Unbound `bind` ** ---- Some of the best '''bind''' coding doesn't involve '''bind''' at all. [DKF], for example, astutely remarked that Some of the best `bind` coding doesn't involve '''bind''' at all. [DKF], event add <> trumps ====== bind $class [bind $type <>] bind $class [bind $type <>] ---- Bind to a plain keystroke only (not modified by Ctrl or Alt): proc bind'plainkey {tag key script} { bind $tag { } bind $tag { } bind $tag $key $script } bind'plainkey all x {puts Hello} [Key-press names] ts Hello} ---- If bind leave command to main window it is not (bindings) grabbing to its associated widgets. e.g.: toplevel $wa -bd 1 -relief ridge ; wm geometry $wa 600x40+$mousex+$mousey ; wm resizable $wa 0 0 ; wm overrideredirect $wa yes ; wm iconname $wa "menu" ; wm group $wa . ; focus $wa ; grab set $wa ; entry $wa.ent01 \ -width 6 -background white entry $wa.ent01 -width 6 -background white bind $wa <1> [list after cancel destroy .base] bind $wa [list destroy .base] But it is destroying base if cursor goes near to entry. Can anybody solve the problem. ---- 2008-03-17: How can I disable pasting into a text widget? I tried the ---- [Tk syntax help] - [Arts and Crafts of Tcl-Tk Programming] [[ [Category Command] | [Category Introspection] | [Category Characters] ]]