User-defined keys John Burton, Rugby, Warwickshire. This program for the 48K Spectrum provides 33 user-defined function keys which are used in a similar way to those on the BBC Micro. Because there are no unused keys on the Spectrum key- board, a new shift key is used to access them. The Space key is used in the same way as the two existing shift keys. It, and any other key pressed together will produce the required line in the edit/input area at the bottom of the screen. For example, if the H key was programmed with: BORDER 7: PAPER 7: INK 0: CLS Pressing Space and H together will produce that line when the next key is pressed. Programming the keys is usually only done when the pro- gram to set up the machine code is run, but could be done using Poke also. After program 1 is entered, key definitions should be entered from line 3000 onwards. This should have the code for each keyword or letter or symbol with the last one being zero. For example the line shown above would be: 3000 DATA 231,55,58,218,55,58,217,48,0 where 231 is the code for border, 55 the code for 7, and 58 the code for : and so on. The first Data line after 3000 will define key H, the second will define Y and the third 6. The sequence is: H Y 6 5 T G V (N) J U 7 4 R F C (M) and so on. The letters in brackets cannot be programmed but must still be given the code 0 in the Data lines. [ "And so on" is good. You need a bit of information on the internals of the Speccy to understand quite what the order is, here. In fact, it's the order as produced by the ROM Key-Scan routine, which Basic programmers never see, as it is immediately converted first to a key symbol, and then to a character depending on the input mode. This key order is related to the physical arrangement of the electronical contacts on the keyboard membrane, and (therefore) also to the values of the IN calls used to read it. A little help to get this key order in, well, order, is on the TZX which goes with this text, described below. One thing which should be explained here: the reason that N, M and so on cannot be defined is that they are on the same section of membrane as the space key itself. (The reason they still have to be defined is presumably that otherwise, the routine would have to take them into account and skip when necessary - and extra work is something a Spectrum interrupt routine can really do without.) ] The codes are put in the area of memory from 60032 on with each key having 32 bytes. The machine code uses interrupts and so cannot be used with another program that does so also. Examples of key definitions: Key H: LOAD "": RUN Key Y: PLOT 0,0: DRAW 255,0: DRAW 0,175: DRAW -255,0: DRAW 0,-175 Key 6: -not used Key 5: PRINT #0; Key T: PLOT 55, 27: DRAW 50,50,105*PI Key G: RANDOMISE 0 The data lines for these are: 3000 DATA 239,34,34,58,247,0 3001 DATA 246,48,44,48,58,252,50,53,53,44,48,58, 252,48,44,49,55,53,58,252,45,50,53,53,44,48,58, 252,48,44,45,49,55,53,0 3002 DATA 245,35,48,59,0 3003 DATA 246,53,53,44,50,55,58,252,53,48,44,53, 44,49,48,53,42,167,0 3004 DATA 249,48,0 To use the program: 1. Type in program one 2. Type in key definitions from line 3000 3. RUN the program 4. Type RANDOMISE USR 65122 Step 4 should be used to re-activate the interrupts when- ever New is used. The program should be saved to tape after step 2 with SAVE "USER KEYS" Once all the steps have been followed, the program and key definitions can be saved with: SAVE "USER CODE" CODE 60000, 5535 To load back type: 1. Clear 59999 2. LOAD "USER CODE" CODE 3. RANDOMISE USR 65122 [ The TZX is arranged a bit differently, for convenience. It would be not very useful if you got the sample lines from the article every time you load the program. Therefore, there are three programs. The first, which is the main loading program, is called "UKeys main". The second and third only contain data lines for the key definitions. To use these, merge one of them with the main program, then RUN. The Out of DATA error you get is OK. Then refer to the procedure described in the article. If (more usefully) you want your own key defi- nitions, just load "UKeys main" and add your own data lines, as described. "UkeySample" contains the sample data lines shown above. Note that line 3001 contains more than 32 bytes. This means that the code for "space-5" spills over into that for "space-6", which is why the latter is described as not being used. In fact, you can use space-6 with these definitions, it just doesn't do anything useful: you get "75", the final two characters of the definition for "space-5". "Ukeys nrs" contains a little something to help make sense of the order in which the keys should be defined. All it does is set up the first key in the order to insert 01, the second 02, and so on until the last key in the key order inserts 39. Three things to be noted: there are 40 keys on the original Speccy keyboard, but the order starts at 00; you cannot, however, get key 00, nor 8, 16, 24 or 32, for the reason mentioned above; and you can't get key 39, either, because that's Caps Shift Space Bar, which gets filtered out before it reaches the User Defined Keys interrupt to serve as Break. ]