Unit:
AxTerm
Description:
The TApxKeyboardMapping class provides a simple, convenient method to specify the PC keystrokes that map onto the emulated terminal keystrokes, and also what control sequence those terminal keystrokes are going to send to the host computer.
There are three parts to the keyboard mapping. The first part is merely for convenience; it is a mapping of the keyboard's virtual key codes to the virtual key names. This mapping is standard and can be found in TrollTech's Qt documentation. An example of a single mapping is to specify that the virtual key code $1030 is the F1 key, which is usually known by the name "Key_F1" in the Qt documentation.
The second part of the keyboard mapping is the definition of the character or control sequence that is sent by the original keyboard to the host computer when a key is pressed. For a VT100 terminal, for example, pressing the PF1 key will either send an <Esc>P or an <Esc>OP sequence to the host, depending on the mode the terminal is in at the time. Again, this mapping is standard and is provided as part of the terminal manufacturer's documentation.
Whereas the two mappings just described are fixed by standards, the last mapping is where the creativity and individuality comes in. This set of mappings details which PC keystroke is mapped to which terminal keystroke. There are no standards for this (though some mappings should be fairly obvious: for example, the up arrow key should be mapped to the terminal's up arrow key), and so you can customize to a large extent which keys perform which action using this third mapping.
The TApxKeyboardMapping class is designed for use by a terminal emulator. The terminal component will trap the user's keystroke and pass it onto the emulator. The emulator will lookup the virtual key code in the keyboard mapping table and will retrieve the virtual key name. It will then prefix this name with the names of the shift keys that are active, and lookup this combination in the keyboard mapping class. If the lookup succeeds, the keyboard mapping class returns the name of a terminal key and this, in turn, can be looked up to find the character or control sequence that should be sent to the host computer. If at any time a lookup fails, there is no special mapping for the keystroke and so the default action takes over (for example, for an alphabetic key, the relevant character is sent to the host computer).
Although it may seem excessive to have three lookups per keystroke to get to the final sequence to send to the host, in reality the design it provides a balance between fast conversion of keystrokes and ease of specification of the various mappings. The lookups are performed using a hash table, which is considered the data structure of choice for this kind of operation.
The TApxKeyboardMapping class is convenient to use when you have to specify a large number of mappings. There are two methods for loading a set of mappings: first from a specially, yet simply, formatted text file, and, second, from a resource within the application. Thus, it is possible to have a default mapping linked into the application, but also to be able to provide a way of altering the mappings at run-time. To help create the resource, the class also has a method to write its current mapping set to a binary file, which can then be compiled into a resource file. The following code example shows this process:
var
KeyMap : TApxKeyboardMapping;
begin
KeyMap := TApxKeyboardMapping.Create;
try
KeyMap.LoadFromFile('AXKEYVT1.TXT');
KeyMap.StoreToBinFile('VT100.BIN');
finally
KeyMap.Free;
end;
end;
The code creates a TApxKeyboardMapping instance called KeyMap. A set of mappings is then read from a file called AXKEYVT1.TXT (this file is a default set of keyboard mappings for a VT100 emulator that is provided with Async Professional CLX). The mappings are then written out to a file called VT100.BIN.
This latter file can be compiled into a resource using a standard Windows resource compiler (such as BRCC.EXE or BRCC32.EXE supplied with Delphi and C++Builder). At the time of this writing, a Windows resource compiler is not supplied with Kylix.
The resource script (RC file) required for this is as follows:
MyVT100KeyMap RCDATA VT100.BIN
If you name the resource file VT100.RC, the resource compiler will create a file called VT100.RES. Adding the resource to your application at that point is merely a case of adding the following line to your project file and recompiling:
{$R VT100.RES}
Now you can call the LoadFromRes method of your TApxKeyboardMapping instance to load this set of keyboard mappings.