|
WikiDoc |
The [binding] command
IntroductionThe [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. SynopsisThe 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 comboMany 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:
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 Binding subcommandsThe [create] subcommandThis 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] subcommandThis 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] subcommandThis 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] subcommandThis 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:
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 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'. Character codes vs. virtual keycodesA 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:
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 namesHere 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):
In the case of key pad bindings, there is no glyph provided by the system. Alpha uses the character glyph instead. Composite bindingsThe -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 codesHere 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.
ExamplesHere 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...) |
|
Edit -
History -
Print -
Recent Changes -
Search
Page last modified on September 30, 2009, at 09:05 AM
|
Hosted on
|