For assigning actions to keys a configuration language has been designed.
It is mostly based on the configuration language of Marko Macek's text editor
FTE.
Describing key code seems to be basic part of such a language. So let
us start with it. Here is the grammer in BNF format:
The modifier declares states of Shift, Control
and Alt keys. The plus and minus signs
determine if such modifier should be pressed (plus) or its state
does not matter (minus).
For example use:
[C+S+A-0]
if you would like to test if Ctrl and Shift are down together with
key '0' and you do not care about the Alt.
[C+S+A+X] to test if all modifiers are down and key
X is pressed
[Z] means code Z and no modifier must be down
[C-A-S-Z] means Z again but the editor does not care about
state of modifiers
The key part identifies the key that should be pressed. For these
purposes digits (0, 1, ..., 9), literals (A, ..., Z) and special
constants (
HOME, END, LEFT, RIGHT,
UP, DOWN, PGUP, PGDN, ENTER,
DEL, BACKSP, ESC, F1,..., F12)
can be used.
As example [S+ENTER] means shift combined with enter.
[S+S] means key S together with shift modifier.
Not only one key but also a sequence of keys can be defined. To achive this you
need only to join two key definition by underscope. As an example let's
show how the clasic WordStar block copy sequnce can be described
in our language:
[C+K_C] says that first of all you need to press
key K together with Ctrl and then release all modifiers and press key C
[C+K_C-C] seems to be a better solution. Because the second
key can now be pressed with or without Ctrl and this solves common
problem of releasing Ctrl before pressing C.
It is of course possible to define longer sequences. There are no limitations
or restrictions.
Assigning actions
Actions are predefined operations with the editor. To follow the
Java spirit and achive maximum flexibility each
actions is represented as one class. The predefined standard actions
are stored in package xelfi.editor.actions but this
is not requiered. Action can be any Java class
in any package that
implements xelfi.editor.keys.KeyActionHandler interface.
To assign a action to for example left arrow the following code can be
inserted into configuration file.
The first line says that on LEFT arrow action MoveLeft (that moves
the cursor one character to the left) should be called. The second line
claims that on LEFT arrow together with Shift key the three actions
should be executed with result that they extends marked block.
Because actions are valid Java classes stored in packages the standard
Java notation can be used to access them.
xelfi.extention.OpenBrowser describes action that
is in class OpenBrowser in package xelfi.extention.
(this is only example, no such action realy exists).
xelfi.editor.actions.MoveRight is action in class
MoveRight in package xelfi.editor.actions.
MoveRight without any package specification also
describes action in xelfi.editor.actions.MoveRight.
That is because the package xelfi.editor.actions is
always imported.
It is possible to include other packages and actions too. And for this
purpose command import can be inserted into configuration file.
As mention before remember that xelfi.editor.actions is
always imported.
To actions parameters can be passed. The type of parameters depends on
action that should accept them but in generaly parameter can be of type
string, character or integer number. As an
example let us have a look at function InsertText that inserts text
at cursor possition. The text is taken from its parameter.
// instead of new line text
// Greetings from editor
// is displayed.
key [ENTER] { InsertText "Greetings from editor"; }
Because default action on many keys is insert of itself
it is not requiered to write such action for each alphanumeric character.
Instead the editor inserts the character automaticly when no other
action is assigned.
Default editor actions
Default actions are predefined in package
xelfi.editor.actions and because this package is allways
imported they can be referenced without package prefix.
BackSpace deletes one character before cursor.
If there is no nonspace character before the cursor till begin of line,
the cursor aligns with the begin of word on previous line.
At the end of this tutorial we would like to encourage you to write us
if you create your own key configuration file. Do not wait if you have
something useful that could come handy for others too.