Detecting Multiple Keys

Developer(s): Mather White
Date: 1984
Type: Program
Platform(s): ZX81, TS 1000
Tags: Utility

The first program shows the keyboard matrix code in the system variable LAST_K (addresses 16421 and 16422) for each key you press. Hold a key down until the numbers appear: the program prints the key, then each byte in decimal and in binary. A 0 bit marks a pressed key’s half-row or data line. Compare the patterns with the charts in the article.

INKEY$ returns an empty string when more than one key is down. The second program is the article’s 15-byte machine code routine in a line 1 REM. PRINT USR 16514 gives 0 while the selected keys are all held, and 16514 otherwise. As printed, the bytes test for S, G and A together. To test other keys, POKE the complemented bit patterns into 16519 and 16523, as the article explains. In your own programs use IF NOT USR 16514 THEN … to check for two-player or combination-lock key presses.

The article has you enter the bytes into the REM with a separate loader. The download has them in place.

Published in Syntax February 1984 (Detecting Multiple Keys).

Image Gallery

Source Code

Keyboard pattern program:
  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

Multi-key test (the line 1 REM holds the 15 bytes 42 37 64 125 230 2 192 124 230 38 192 1 0 0 201):
   1 REM 123456789012345
   2 PRINT USR 16514
   3 SCROLL
   4 GOTO 2

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.