Keyboard Input Or Controlled Escape

Brian Van Cleve

The BASIC INPUT statement is prone to user error. What is needed is controlled input, where each key is checked for validity. The following program even checks for the START key to permit an "escape" function.

Here is a short subroutine that I use in all my menu driven programs. It allows the user to enter data as usual while checking for the start key being pressed. If it discovers use of the start key, the subroutine provides a controlled escape.

To use this, DIM IN$ to the maximum expected input and open the keyboard for input using OPEN #1, 4, 0, "K : ". Set the variable KEY to the first line number in the subroutine and the variable ESC to the line to return to if the user presses the START key (like the main menu, for example). Then use a GOSUB KEY for any input. The user input will be returned in IN$.

Now for the program.

PROGRAM. Keyboard Input

1000 REM KEYBOARD		I use REM as the first statement in
 INPUT			 my subroutines, easy to move
1005 IN$ = ""			Set IN$ to null
1010 POKE 764, 255		Clear keyboard buffer out
1020 POKE 756, 224		Force uppercase letters
1030 POKE 694, 0		NO inverse video
1050 IF PEEK(53279) = 6		START key is pressed
 THEN POP : GOTO ESC		Pop the return off stack and esc
1060 IF PEEK(764) = 255		No key pressed yet
 THEN 1050
1070 GET #1, K			Key pressed, get it
1080 IF K = 155 THEN		Pressed return key
 RETURN
1090 IN$(LEN(IN$) + 1)		Put key in string
 = CHR$(K)
1100 ? CHR$(K);			Print it on screen
1110 GOTO 1050			Loop til done

Return to Table of Contents | Previous Section | Next Section