Beginners’ BASIC: More on INKEYS

Author(s): Ann Zevnik
Publication: Syntax
Pub Details: Syntax/Syntax ZX80 v3 n7 (Volume 3, Issue 7)
Date: July 1982
Pages: 5

As we saw in May 82, the 8K ROM’s INKEY$ function “watches” the keyboard and looks for a keypress. When used with PAUSE, INKEY$ tells the computer to wait for the user to press something.

You can use INKEY$ to either make the computer stop program execution if any key is pressed, or stop unless any key is pressed.

In either case, remember the PAUSE statement that makes the computer wait. Because you want to control when the program stops or continues, use a nice big value like PAUSE 40000. This gives you enough time to choose what to do before the computer goes on without you–the bigger the value, the longer the PAUSE.

To stop execution if any key is pressed, add a line like this after the PAUSE statement:

 100 IF INKEY$<>"" THEN GOTO 100

This line checks to see if any key is pressed. The null string (“”) means no key. Thus any keypress at line 100 (making the IF part true) causes the computer to GOTO 100. As long as you hold a key, the program loops back on itself at line 100. This “holding pattern” effectively makes the computer wait until you let up on the key.

This method also lets you program a keyboard-controlled reset. At any time in your program, you can insert a PAUSE and INKEY$ combination to allow the user to stop using the program or to start over. Change line 100 to IF INKEY$<>”” THEN STOP to end the program. Or use IF INKEY$<>”” THEN GOTO the first program line.

To stop execution unless any key is pressed, use this line:

 100 IF INKEY$="" THEN GOTO 100

Here the null string, or no key, causes the computer to stay at line 100. Now the computer waits until a key is pressed (so INKEY$ does not equal “”) to go on.