INKEY$ returns the character of the key currently being pressed, returning the empty string for no key and for multiple keys. But you can use the system variables and a few bytes of machine code to detect multiple keys of your choosing.
Memory locations 16421 & 16422 store a keyboard matrix code for the keys currently held down. Your ZX/TS system calls these system variables LAST_K; they have no direct counterpart in Spectrum and 2068 machines.
Contents of 16421 16422 KEY Bit=0 Bit=0 ZXCV 0 SHIFT 0 ASDFG 1 AQ10P## 1 QWERT 2 ZSW29OL. 2 12345 3 XDE38IKM 3 09876 4 CFR47UJN 4 POIUY 5 VGT56YHB 5 #LKJH 6 (# means shift, #.MNB 7 enter, or break)
Both locations are all ones (255 dec.) when no key is pressed. When you push a key, one or more bits in each system variable become zero. For example, depressing B makes bit 7 of 16421 and bit 5 of 16422 go to zero. All other bits remain set to 1. So the contents of these locations for B equal:
(16421)=01111111=127 (16422)=11011111=223
Only the SHIFT key works with a different pattern–bit 0 of 16422 becomes 0 while all bits of 16421 remain 1. In typing, you use SHIFT with another key, and the exception permits the ZX/TS to allow one two-key pair while rejecting other combinations.
Thus, SHIFT B returns:
(16421)=01111111=127 (16422)=11011110=222
Use the following program to see these patterns on your screen. Compare the results to the charts preceding.
10 GOTO 1000
30 LET K$=INKEY$
40 IF K$="" THEN GOTO 30
50 RETURN
60 SCROLL
70 LET Z=16421
80 LET A(S)=PEEK Z
90 LET A(S+S)=PEEK (Z+S)
95 PRINT K$;S$;
100 FOR D=S TO S+S
110 LET A=A(D)
120 PRINT A;S$;
130 FOR F=7 TO 0 STEP -1
140 LET X=2**F
150 PRINT A>=X;
160 IF A>=X THEN LET A=A-X
170 NEXT F
180 PRINT ,
190 NEXT D
200 GOSUB 30
299 GOTO 60
1000 DIM A(2)
1100 LET S$=" "
1200 LET S=PI/PI
1500 GOSUB 30
1999 GOTO 60
SYNTACTIC SUM: 22782, 8K ROM
A 253 11111101 253 11111101 S 253 11111101 251 11111011 D 253 11111101 247 11110111 F 253 11111101 239 11101111 G 253 11111101 223 11011111 V 254 11111110 223 11011111 G 253 11111101 223 11011111 T 251 11111011 223 11011111 5 247 11110111 223 11011111 6 239 11101111 223 11011111 Y 223 11011111 223 11011111 H 191 10111111 223 11011111 B 127 01111111 223 11011111 > 127 01111111 246 11110110 + 191 10111111 246 11110110 ( 223 11011111 246 11110110 I 223 11011111 247 11110111 K 191 10111111 247 11110111 M 127 01111111 255 11111111
When you run this program, hold the key down until the first decimal value prints.
Now we can construct a machine code routine to detect multiple-key presses. But first we will need to choose a specific set of keys to test for. Use the preceding charts to see which bits should equal 0. (Convert them to decimal if you wish.) Now change all the ones to zeros and all the zeros to ones. (Subtract from 255 if you are using decimal.) POKE these new values in locations 16519 and 16523.
For example to test for S, G & A, you must decide which bits to zero. From the preceding tables we see these required zeros:
16421 16422 Letter Bit=0 Bit=0 S 1 2 G 1 5 A 1 1
So we need the following:
(16421)=11111101=253 (16422)=11011001=217
To get the values needed for our program, we must reverse all 1s and 0s, bit by bit (complement the number). You can do this using the decimal values by subtracting them from 255. Thus:
11011001=217 +00100110= 38 11111111=255
So we will use 2 and 38 as values to put into our sample MC program.
Now use the following machine code routine to test for a specific combination of depressed keys.
1 REM E9RND? NEW ▒""? NEW A""▒ TAN
SYNTACTIC SUM: 1848, 8K ROM
42 37 64 125 230 2 192 124 230 38 192 1 0 0 201
Now enter:
2 PRINT USR 16514
3 SCROLL
4 GOTO 2 RUN
When you press the selected keys, you’ll see a zero on-screen, otherwise you get 16514. So if you use IF NOT USR 16514 THEN…instead of IF INKEY$=…THEN…, you can test for more than one key. This extends your ability to write two-player games or to make combination locks for your programs.
[Values stored in 16421 & 16422 are related to the numbers we generated in KEYBOARD SCANNING. Except for the SHIFT key, the content of 16421 equals the B-register value that scans a half-row. Bits 1 through 5 of 16422 equal the data line inputs D0-D4. For the SHIFT key, 16421 holds 255, and 16422 holds 254. Cassette input values do not show up in these variables, despite their appearance in keyscans.–KO]
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.