Sometimes when writing a BASIC program, you want to offer the user a menu choice of actions. Several methods let you input data, but often programmers prefer the INKEY$ function. It does not require the user to press ENTER after his or her response. INKEY$ does have its disadvantages–it’s sometimes slow to respond. Additionally, if you program for novice users, you should remember that pressing the BREAK key causes your BASIC program to stop. An inexperienced user may not be able to start the program again without destroying data (if not the program itself).
This machine code routine returns the CODE value of the key pressed to your BASIC program. You can POKE it to ignore any entries except for given low and high parameters. This routine shows how to use the ROM subroutines that scan the keyboard and the routine that decodes the scan.
Before calling this routine, put the computer in SLOW mode as the display disappears if RUN in FAST. You can relocate the subroutine anywhere; it’s usable in any location because it contains all relative jump instructions.
Start by entering the BASIC program shown in Listing 1. Now, hit RUN then ENTER without entering a line number.
1 REM 1234567890123456789012345678901
100 LET X=16514
110 LET A=0
120 FOR K=0 TO 30
130 PRINT (X+K);" ";
140 INPUT B
150 POKE (X+K),B
160 PRINT PEEK (X+K)
170 LET A=A+PEEK (X+K)
180 NEXT K
190 PRINT A
200 POKE 16510,0
210 STOP
SYNTACTIC SUM: 11580, 8K ROM
On the screen you’ll see a memory address and an L prompt. In response to each prompt, carefully enter the decimal numbers from the following list, starting at the top and working from the left along each row. Hit ENTER after each #.
205 187 2 44 32 250 205 187 2 68 77 81 20 40 247 205 189 7 126 254 29 56
Now you should get an out-of-screen-memory report 5/130. Press CONT then ENTER, and type these #s.
233 254 38 48 229 78 6 0 201
You now see report 9/210 at the screen bottom. If you entered all the numbers correctly, the # 3600 appears just under the listing.
Press LIST and ENTER to see the listing again. The first line:

Now enter the demo program in Listing 2 and press RUN 100 ENTER.
1 SLOW
2 LET J=(USR 16514)-28
3 FAST
4 RETURN
100 PRINT "CHOOSE:"
110 PRINT "1. CHOICE ONE"
120 PRINT "2. CHOICE TWO"
130 PRINT "3. CHOICE THREE"
140 PRINT "4. CHOICE FOUR"
150 PRINT "5. CHOICE FIVE"
160 PRINT "6. CHOICE SIX"
170 PRINT "7. CHOICE SEVEN"
180 PRINT "8. CHOICE EIGHT"
190 PRINT "9. CHOICE NINE"
200 GOSUB 1
210 PRINT ,,"CHOICE= ";J
220 GOTO 200
SYNTACTIC SUM: 20000, 8K ROM
Try pressing keys 1-0 along the top row, then other keys on the keyboard. You see that the routine works; the computer now accepts only the choices displayed on the screen as valid input.
To change the parameters, or what input the computer will accept, enter these lines with no line number, substituting the proper values for low and high limit:
POKE 16534,CODE (low limit) POKE 16538,CODE (high limit + one)
Remember to use the CODE and not the number itself. See the back of your ZX/TS manual for a complete list of the character set codes.
Following is a commented list of the machine language in this subroutine.
Decimal
Address Decimal Hex Mnemonic Comments
START 16514 205 CDBB02 Call 02BBH ;Call ROM scan keyboard
187 ;routine.
2
16517 44 2C INC L ;Check if user's finger
;on key.
16518 32 20FA JR NZ,START ;If so, go back and check
250 ;again.
AGAIN 16520 205 CDBB02 Call 02BBH ;Call ROM scan keybd.
187
2
16523 68 44 LD B,H ;Pass result of keybd
16524 77 4D LD C,L ;scan in HL to BC.
16525 81 51 LD D,C ;Use D reg. to check for
16526 20 14 INC D ;no entry (no entry
;C=FFh).
16527 40 28F7 JR Z,AGAIN ;If D=0 then C must have
247 ;been FFh. Scan again.
16529 205 CDBD07 Call 07BDH ;Call ROM decode keybd
189 ;routine.
7
16532 126 7E LD A,(HL) ;HL now contains address
;pointing to keybd input,
;thus we load contents of
;this address into accum.
16533 254 FE1D CP 1DH ;Compare to low parameter.
29
16535 56 38E9 JR C,START ;If carry set, then input
233 ;is below low parameter.
16537 254 FE26 CP 26H ;Else, check high
38 ;parameter.
16539 48 30E5 JR NC,START ;If carry not set, then
229 ;input is above high
;parameter.
16541 78 4E LD C,(HL) ;Else, input OK. Load into
;C register.
16542 06 0600 LD B,00 ;Clear B register to 0 as
00 ;the BC register is the
;value returned to BASIC
;calling routine.
16544 201 C9 RET ;Return to BASIC.
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.