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.
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:
{c 'A'} is the same as {c 65}.
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.
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:
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.
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.
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:
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'.
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:
| Description | Keycode |
| Down Arrow | 125 |
| Left Arrow | 123 |
| Right Arrow | 124 |
| Up Arrow | 126 |
| Delete | 51 |
| End | 119 |
| Enter | 76 |
| F1 | 122 |
| F2 | 120 |
| F3 | 99 |
| F4 | 118 |
| F5 | 96 |
| F6 | 97 |
| F7 | 98 |
| F8 | 100 |
| F9 | 101 |
| F10 | 109 |
| F11 | 103 |
| F12 | 111 |
| F13 | 105 |
| F14 | 107 |
| F15 | 113 |
| Forward Delete | 117 |
| Help | 114 |
| Home | 115 |
| Num Lock | 71 |
| Page Down | 121 |
| Page Up | 116 |
| Return | 36 |
| Tab | 48 |
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.
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):
| Name | Keycode | Glyph |
| Blank | 0x31 | 0x61 |
| Clear | 0x75 | 0x1c |
| Del | 0x33 | 0x17 |
| Down | 0x7d | 0x6a |
| End | 0x77 | 0x69 |
| Enter | 0x4c | 0x4 |
| Esc | 0x35 | 0x1b |
| F1 | 0x7a | 0x6f |
| F2 | 0x78 | 0x70 |
| F3 | 0x63 | 0x71 |
| F4 | 0x76 | 0x72 |
| F5 | 0x60 | 0x73 |
| F6 | 0x61 | 0x74 |
| F7 | 0x62 | 0x75 |
| F8 | 0x64 | 0x76 |
| F9 | 0x65 | 0x77 |
| F10 | 0x6d | 0x78 |
| F11 | 0x67 | 0x79 |
| F12 | 0x6f | 0x7a |
| F13 | 0x69 | 0x87 |
| F14 | 0x6b | 0x88 |
| F15 | 0x71 | 0x89 |
| ForwardDel | 0x75 | 0xa |
| Help | 0x72 | 0x67 |
| Home | 0x73 | 0x66 |
| Kpad* | 0x43 | 0x2a |
| Kpad+ | 0x45 | 0x2b |
| Kpad- | 0x4e | 0x2d |
| Kpad. | 0x41 | 0x2e |
| Kpad/ | 0x4b | 0x2f |
| Kpad0 | 0x52 | 0x30 |
| Kpad1 | 0x53 | 0x31 |
| Kpad2 | 0x54 | 0x32 |
| Kpad3 | 0x55 | 0x33 |
| Kpad4 | 0x56 | 0x34 |
| Kpad5 | 0x57 | 0x35 |
| Kpad6 | 0x58 | 0x36 |
| Kpad7 | 0x59 | 0x37 |
| Kpad8 | 0x5b | 0x38 |
| Kpad9 | 0x5c | 0x39 |
| Kpad= | 0x51 | 0x3d |
| Left | 0x7b | 0x64 |
| NumLock | 0x47 | 0x1c |
| PageDown or PgDn | 0x79 | 0x6b |
| PageUp or PgUp | 0x74 | 0x62 |
| Return | 0x24 | 0xb |
| Right | 0x7c | 0x65 |
| Space | 0x31 | 0x9 |
| Tab | 0x30 | 0x2 |
| Up | 0x7e | 0x68 |
In the case of key pad bindings, there is no glyph provided by the system. Alpha uses the character glyph instead.
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.
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.
| Name | Dec value | Description |
| Apple Logo Filled | 20 | The filled Apple logo. |
| Apple Logo Outline | 108 | The outlined Apple logo. |
| Blank | 97 | The blank key. |
| Caps Lock | 99 | The Caps Lock key. |
| Checkmark | 18 | The Check mark key. |
| Clear | 28 | The Clear key. |
| Command | 17 | The Command key. |
| Contextual Menu | 109 | The contextual menu key |
| Control | 6 | The Control key. |
| Control ISO | 138 | The ISO standard control key. |
| Delete Left | 23 | The Delete-to-the-left key. |
| Delete Right | 10 | The Delete-to-the-right key. Used in right-to-left script systems. |
| Diamond | 19 | The diamond mark. |
| Down Arrow | 106 | The down arrow key. |
| Downward Arrow Dashed | 16 | The downward dashed arrow key. |
| Eject | 140 | The Eject key (available in Mac OS X v10.2 and later). |
| Enter | 4 | The Enter key. |
| Escape | 27 | The Escape key. |
| F1 | 111 | The F1 key. |
| F2 | 112 | The F2 key. |
| F3 | 113 | The F3 key. |
| F4 | 114 | The F4 key. |
| F5 | 115 | The F5 key. |
| F6 | 116 | The F6 key. |
| F7 | 117 | The F7 key. |
| F8 | 118 | The F8 key. |
| F9 | 119 | The F9 key. |
| F10 | 120 | The F10 key. |
| F11 | 121 | The F11 key. |
| F12 | 122 | The F12 key. |
| F13 | 135 | The F13 key. |
| F14 | 136 | The F14 key. |
| F15 | 137 | The F15 key. |
| Help | 103 | The Help key. |
| Left Arrow | 100 | The left arrow key. |
| Left Arrow Dashed | 24 | The dashed left arrow key. |
| Nonmarking Return | 13 | The nonmarking Return key. Note that this glyph has no visible representation (that is, nothing appears in the menu). |
| Northwest Arrow | 102 | The northwest arrow key |
| Null | 0 | The null character. Note that this glyph has no visible representation (that is, nothing appears in the menu). |
| Option | 7 | The Option key. |
| Page Down | 107 | The Page Down key. |
| Page Up | 98 | The Page Up key. |
| Pencil | 15 | The Pencil key. |
| Power | 110 | The power key (that is, the startup key). |
| Return | 11 | The Return key for left-to-right script systems. |
| Return R2L | 12 | The Return key for right-to-left script systems. |
| Right Arrow | 101 | The right arrow key. |
| Right Arrow Dashed | 26 | The dashed right arrow key. |
| Shift | 5 | The Shift key. |
| Southeast Arrow | 105 | The southeast arrow key. |
| Space | 9 | The Space bar. Note that this glyph has no visible representation (that is, nothing appears in the menu). |
| Tab Left | 3 | The Tab-to-the-left key. Used in right to left script systems. |
| Tab Right | 2 | The Tab-to-the-right key. Used in left to right script systems. |
| Up Arrow | 104 | The up arrow key. |
| Up Arrow Dashed | 25 | The 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...)