From AlphaWiki

Commands: The [binding] command

This file documents the new [binding] command introduced in version 8.2a1d1 of AlphaX.


The [binding] command lets you create and manipulate key bindings in Alpha. The [binding] command is a proposed replacement for the old [bind] (or [Bind]) command.

Bindings are associated with Tcl procs (or Tcl code snippets): this is a characteristic they share with menu items which can also be associated with Tcl procs using the new [menuItem] command (see the MenuItemCommand page on this wiki). The purpose of this new approach is to resolve the conflictual situations caused by key combinations defined directly by menu definitions or by an explicit call to the old [Bind] command.

A key binding is defined by the following attributes:

A key binding is uniquely determined by these parameters. The code and the modifiers are required parameters. The other parameters are optional. By default, a code is interpreted as a character code. If the tag is empty or unspecified, the binding is global.


The formal syntax of the [binding] command is:

binding subcommand ?options?

The possible subcommands are described below. Depending on the subcommand, various options can be additionnally specified.

Specifying a key combo

Many subcommands of the [binding] command take a command key argument of the form

     {modifiers key}

This argument describes a key combination. It is a two elements Tcl list:

The modifiers are specified as a string made of some of the following letters:

"c"corresponds to the Command key
"s"corresponds to the Shift key
"o"corresponds to the Option key
"z"corresponds to the Control key
"v"specifies whether the main key is to be interpreted as a character code or as a virtual key code (see the Character codes vs. virtual keycodes section below).

The modifier string must be an empty string if no modifiers are associated with the key combo.

There are three ways of specifying the main key:

Note that character code bindings are case insensitive. So Command-a and Command-A are identical. If you really want the Shift key to be part of the binding, you must specify the letter s in the modifier string. For instance, the argument {cs 65} would correspond to the Command-Shift-A combination.

Binding subcommands

The [create] subcommand

This subcommand creates a new binding or redefines an already existing binding. It accepts a few options. The complete syntax is:

     binding create ?option value...? {modifiers key} proc

The command has two required arguments:

Here are the currently defined options:

The [delete] subcommand

This subcommand lets you delete a key binding. The syntax is:

     binding delete ?option value...? {modifiers key}

The possible options are -tag and -prefix which have the same meaning as with the [binding create] command.

The [info] subcommand

This subcommand lets you get information about an existing key binding. The syntax is:

    binding info (command|help) ?option value...? {modifiers key}

The third argument indicates which info is desired. It can be one of the following keywords:

The possible options are -tag and -prefix which have the same meaning as with the [binding create] command. If the binding does not exist, the command raises an error.

The [list] subcommand

This subcommand returns a list of the currently existing key bindings. The syntax is:

     binding list ?option value...?

One can specify some options to restrict the returned list to only the bindings satisfying these options. The available options are:

-command, -creator, -glyph, -key, -modifiers, -prefix, -tag, -virtual

This allows you to specify key codes or modifiers separately. The value of the -key option can be either a numeric value or a letter enclosed between single quotes. The value of the -modifiers option is a string made of the c, o, s, z, v letters (see the section Specifying a key combo above). The -virtual option can be used to list only character code bindings (if the value is 0) or only virtual keycode bindings (if the value is 1).

Each element of the list returned by the [binding list] command is a sublist describing a particular key binding. The sublist contains 6 or 8 elements:

  1. the modifier string (an empty string if there are no modifier specifications)
  2. the key code (decimal value)
  3. the tag (an empty string if the binding is global)
  4. the creator (an empty string if no creator was specified)
  5. the Tcl script attached to the binding
  6. the help string attached to the binding
  7. the modifier string of the prefix (empty if there are no modifier specifications)
  8. the key code of the prefix (decimal value)

The last two elements are present only if the binding is composite and has been defined with a -prefix option. For instance, if a binding is created like this:

     binding create -creator "abcd" -tag Tcl {cv 112} Tcl::doSomethingProc

the returned sublist will be

     {cv 112 Tcl abcd Tcl::doSomethingProc ""}

This command can be used to find if there exists some key bindings pointing to a particular Tcl proc. For instance:

     binding list -command Tcl::doSomethingProc

The command can also be useful with the -creator option to identify certain groups of bindings. For instance, a package might want to keep track of the bindings it defines: if the bindings were declared with a -creator option like this

     binding create -creator "ABCD" {modifiers key}

the package can expose them later via:

     binding list -creator "ABCD"

See examples in the Examples section below.

Note that character code bindings corresponding to a letter are case insensitive: they are stored internally using the code of the lowercase letter even if the definition was invoked using an uppercase letter. On output, all the character code bindings corresponding to a printable character are written with this character (uppercase form between single quotes); the other bindings (including virtual key bindings) are represented by the decimal value of the key. So, if a binding has been defined for 'a', the value returned by [binding list] will be 'A'.

Character codes vs. virtual keycodes

A keyboard equivalent may be either a character code or a virtual keycode. Only one is used by the application at any given time: a virtual keycode binding always has precedence over a character code binding.

Here is a list of the main virtual keycodes:

Down Arrow125
Left Arrow123
Right Arrow124
Up Arrow126
Forward Delete117
Num Lock71
Page Down121
Page Up116

You can obtain the value of any virtual keycode using the Key Codes... command found is the Ascii Etc submenu of the Tools menu. Note that zero is a valid virtual keycode.

Symbolic key names

Here is the list of the predefined symbolic names which can be used to specify a virtual key binding. These values are case insensitive, so one can use HOME, home, Home indifferently.

The following table gives the symbolic names, the corresponding key codes and the values of the glyph automatically associated by default by Alpha (unless the -glyph option was specified):

PageDown or PgDn0x790x6b
PageUp or PgUp0x740x62

In the case of key pad bindings, there is no glyph provided by the system. Alpha uses the character glyph instead.

Composite bindings

The -prefix option can be used in the binding create command to define composite bindings. Its value is a two-elements Tcl list of the form {modifiers key} with the same meaning as for the main key argument.

For instance, in order to define the binding ctrl-opt-B Z (which means, first press ctrl-opt-B, then press Z), the instruction is:

     binding create -prefix {oz 'B'} {"" 'Z'} someProc

The ctrl-opt-B Z binding would trigger the Tcl proc designated by someProc.

For this mechanism to work, the simple ctrl-opt-B binding must also be defined and must be attached to the [prefixChar] core command. For instance:

     binding create {oz 'B'} prefixChar

The [prefixChar] core command takes care of waiting for the second part of the binding to be entered (here the letter Z) and then to execute the code designated by someProc.

Glyph codes

Here is a table of the numerical constants which can be used with the -glyph option. Glyphs are used with virtual keycode bindings (not with character code bindings) to represent keys which are not associated with a single letter or symbol on the keyboard. Most of these values have already been listed in the table of the Symbolic key names section above.

Virtual keycode bindings attached to a key corresponding to a simple letter or symbol are represented by this letter or symbol.

You would not normally have to invoke this option unless you wanted to modify the default glyph automatically provided by Alpha. For instance, in some circumstances, you might want to replace the Left Arrowglyph by the Left Arrow Dashed glyph.

NameDec valueDescription
Apple Logo Filled20The filled Apple logo.
Apple Logo Outline108The outlined Apple logo.
Blank97The blank key.
Caps Lock99The Caps Lock key.
Checkmark18The Check mark key.
Clear28The Clear key.
Command17The Command key.
Contextual Menu109The contextual menu key
Control6The Control key.
Control ISO138The ISO standard control key.
Delete Left23The Delete-to-the-left key.
Delete Right10The Delete-to-the-right key. Used in right-to-left script systems.
Diamond19The diamond mark.
Down Arrow106The down arrow key.
Downward Arrow Dashed16The downward dashed arrow key.
Eject140The Eject key (available in Mac OS X v10.2 and later).
Enter4The Enter key.
Escape27The Escape key.
F1111The F1 key.
F2112The F2 key.
F3113The F3 key.
F4114The F4 key.
F5115The F5 key.
F6116The F6 key.
F7117The F7 key.
F8118The F8 key.
F9119The F9 key.
F10120The F10 key.
F11121The F11 key.
F12122The F12 key.
F13135The F13 key.
F14136The F14 key.
F15137The F15 key.
Help103The Help key.
Left Arrow100The left arrow key.
Left Arrow Dashed24The dashed left arrow key.
Nonmarking Return13The nonmarking Return key. Note that this glyph has no visible representation (that is, nothing appears in the menu).
Northwest Arrow102The northwest arrow key
Null0The null character. Note that this glyph has no visible representation (that is, nothing appears in the menu).
Option7The Option key.
Page Down107The Page Down key.
Page Up98The Page Up key.
Pencil15The Pencil key.
Power110The power key (that is, the startup key).
Return11The Return key for left-to-right script systems.
Return R2L12The Return key for right-to-left script systems.
Right Arrow101The right arrow key.
Right Arrow Dashed26The dashed right arrow key.
Shift5The Shift key.
Southeast Arrow105The southeast arrow key.
Space9The Space bar. Note that this glyph has no visible representation (that is, nothing appears in the menu).
Tab Left3The Tab-to-the-left key. Used in right to left script systems.
Tab Right2The Tab-to-the-right key. Used in left to right script systems.
Up Arrow104The up arrow key.
Up Arrow Dashed25The dashed up arrow key.


Here are a few basic examples which can be executed one by one in the Tcl shell in AlphaX:

     proc bindingActionProc {idx}  {
         alertnote "Command $idx triggered"
     # Define a few commands
     for {set i 1} {$i < 12} {incr i} {
         set cmd$i "test::bindingActionProc $i"

     # Bindings to letter A (resp opt-cmd-A, ctrl-cmd-A, and ctrl-opt-cmd-A)
     binding create -help "opt-cmd-A combo" {co 65} $cmd1
     binding create -help "ctrl-cmd-A combo" {cz 65} $cmd2
     binding create -help "ctrl-opt-cmd-A combo" {coz 65} $cmd3

     # Bindings to letter L
     binding create {co 76} $cmd4
     binding create -tag Tcl {co 76} $cmd5
     binding create -tag Python {co 76} $cmd6
     binding create -tag Ruby {co 76} $cmd7

     # A virtual keycode binding attached to opt-F5
     binding create {cozv F5} $cmd8
     binding create -tag Tcl {cozv F5} $cmd9
     binding create -tag Ruby {cozv F5} $cmd10

     # The Help key
     binding create {v Help} $cmd11

     # Get a list of all the bindings
     binding list

     # Get a list of all the bindings associated with letter L
     binding list -key 76
     # Get a list of all the bindings using opt-cmd
     binding list -modifiers "co"
     # Get a list of all the bindings defined in Ruby mode
     binding list -tag Ruby
     # Get a list of all the opt-cmd bindings in Ruby mode
     binding list -modifiers "co" -tag Ruby
     # Get a list of all the bindings attached to act1
     binding list -command $cmd1

     # Find the command associated with the opt-cmd-L combination in Ruby mode
     binding info command -tag Ruby {co 76}

     # Retrieve the help associated with a binding
     binding info help {co 65}

    # Delete the previous combination
     binding delete -tag Ruby {co 76}

(add your comments here...)

Retrieved from
Page last modified on September 30, 2009, at 09:05 AM