The 102 key keyboard of the Archimedes is very nice, but making use of all the keys from within a program can be a bit tricky. In this article I will show how the system can be programmed to allow all the keys to be read. First, a brief description of how the keyboard is handled.
Each time a key is pressed or released the keyboard sends an up-down code to the keyboard handler software within the operating system. The handler then converts this into a buffer code which is placed in the keyboard buffer. When the key value is read from the buffer by the program, it is further translated into a code, or series of codes. The following short program will print out the codes of any keys pressed:
10 REPEAT key=GET
20 PRINT key " (&";~key;")"
30 UNTIL key=17
The buffer codes for the vast majority of keys are exactly the same as the codes returned to the program. These correspond to all the keys which produce printable characters, or standard control codes such as Delete and Return. For example, pressing the 'A' key with the Caps Lock off will enter the value 97 (&61) into the keyboard buffer, and when the program reads this it will read out the value 97 (&61) which is the ASCII code for an 'a'. However, the function keys and cursor keys are very special cases.
Table 1 shows the buffer codes for the function keys (and the cursor keys, but these can be ignored for the moment). From the table, you can see that the code entered into the buffer depends not only on the key pressed, but also the combination of Shift and Ctrl as well. Furthermore, the Print key behaves as if it were function key F0, and the Insert key as if it were F13. When a function key code is removed from the buffer it is not returned directly to the program. Instead, the buffer code is split into two parts - the high and low hex digits (nibbles). For example, the buffer code for key F11 is &CB. This would be split into the high digit &C (12), and the low digit &B (11). The top nibble is then used to look up the action to perform. There are four possible actions:
1. Ignore the keypress.
2. Expand the keypress into a soft key definition.
3. Translate the keypress to a single code.
4. Translate the keypress to a zero value followed by a code.
Basic | Key | Key | Key | ||
Key | Code | +Shift | +Ctrl | +Shift+Ctrl | |
&80 | &90 | &A0 | &B0 | ||
F1 | &81 | &91 | &A1 | &B1 | |
F2 | &82 | &92 | &A2 | &B2 | |
. | . | . | . | . | |
F8 | &88 | &98 | &A8 | &B8 | |
F9 | &89 | &99 | &A9 | &B9 | |
Copy | &8B | &9B | &AB | &BB | |
<Left> | &8C | &9C | &AC | &BC | |
<Right> | &8D | &9D | &AD | &BD | |
<Down> | &8E | &9E | &AE | &BE | |
<Up> | &8F | &9F | &AF | &BF | |
Page Down | &9E | &8E | &BE | &AE | |
Page Up | &9F | &8F | &BF | &AF | |
F10 | &CA | &DA | &EA | &FA | |
F11 | &CB | &DB | &EB | &FB | |
F12 | &CC | &DC | &EC | &FC | |
Insert | &CD | &DD | &ED | &FD |
For each of the eight possible buffer code high digits (&8 - &F), there is a separate *FX call which determines how that buffer code will be interpreted. The appropriate calls are listed in table 2.
*FX n | Range covered |
221 | &C0 - &CF |
222 | &D0 - &DF |
223 | &E0 - &EF |
224 | &F0 - &FF |
225 | &80 - &8F |
226 | &90 - &9F |
227 | &B0 - &BF |
288 | &B0 - &BF |
The action of the keys is set using a command such as:
*FX221,n
where n determines the action for the chosen range of keys. The possible values are:
0 | Ignore the keypress. |
1 | Expand the key as a soft key. |
2 | Return zero followed by buffer code. |
3-255 | Return n + key index. |
The first one of these is self-explanatory - the keypress is ignored. The second option causes the keypress to be expanded into a soft key definition (as defined with *KEY). The individual buffer code determines which key definition is used. For example, *FX226,1 will select the Shift-Function keys to be expanded into soft keys. Pressing Shift F2 will then enter the buffer code &92 (from table 1). The high digit is 9, and therefore the *FX226 value will be looked up (table 2). As we have set this to generate the soft key string, the operating system will take the definition corresponding to the low digit (2), and return this.
The third option causes the keypress to be returned as two codes. The first is a zero which indicates 'Function key code follows', and the second is the buffer code of the key. For example, *FX225,2 will select the function keys (without Shift or Ctrl) to return two-byte codes. If key F8 is now pressed, two codes will be returned - a zero, and an &88. This can be tried out with the program given earlier. The main use of these two byte codes is so that function key presses can be distinguished from foreign characters generated using the Alt key.
The final option allows a single byte code of the form n+x to be returned, where n is the value programmed with the *FX command, and x is the lower digit of the buffer code. For example, *FX225,64 will program the function keys to produce codes from 64 upwards. Hence, the Print key will generate ASCII 64 which is '@', F1 will generate 65 ('A'), F2 66 ('B') etc. Obviously the value of n cannot be 0, 1 or 2, because these values are used to specify the other options.
The default settings for the function keys are shown in table 3. This means that the function keys on their own will be expanded into the programmed strings, while all the other combinations will produce single byte codes (e.g. &81-&89 for Shift F1 to Shift F9) except for Print and F1-F9 pressed in conjunction with Shift and Ctrl, which will be totally ignored.
Range | Action |
&80-&8F | 1 |
&90-&9F | &80 |
&A0-&AF | &90 |
&B0-&BF | 0 |
&C0-&CF | 1 |
&D0-&DF | &D0 |
&E0-&EF | &E0 |
&F0-&FF | &F0 |
You may have noticed that the cursor keys (the arrow keys and Copy) also appear in table 1, in the same group as the basic function keys. However, the behaviour of the cursor keys is slightly more complex. Normally the cursor keys don't cause any value to be entered into the keyboard buffer at all. Instead, they move the input cursor around the screen and Copy 'picks up' the character below the cursor. Their operation can, however, be modified by using the command *FX4:
*FX4 0 | Normal operation |
*FX4 1 | Generate fixed codes |
*FX4 2 | Act as function keys |
The first of these is the default 'editing' mode described above. The second mode of operation causes the cursor keys to return fixed codes regardless of whether Shift and/or Ctrl are pressed. These codes are:
Copy | 135 (&87) |
Left | 136 (&88) |
Right | 137 (&89) |
Down | 138 (&8A) |
Up | 139 (&8B) |
In the third mode, the cursor keys behave just like function keys, adopting the buffer codes listed in table 1.
Finally, the best way to learn how to make best use of the function keys is to experiment with various settings of *FX221-228 and investigate the codes produced, perhaps using the simple program given earlier.