KEY
Format: var = KEY
Supported by: COMX BASIC V1.00
Similar commands:
Floating Point BASIC 2.2: GOKEY / KEY
Pecom 32 BASIC, Pecom 64 BASIC 1.0 and 4.0, Telmac SBASIC v 24.3: KEY
This function returns the ASCII code of a key pressed. Its use may be illustrated by the following example.
10 IF KEY = 65 THEN GOTO 30 20 GOTO 10 30 PRINT "KEY A PRESSED" 40 END
When the function KEY is encountered, the keyboard is scanned, and the. ASCII code of the key pressed is returned. If this is equal to 65 (which is the ASCII decimal code for key A), the message "KEY A PRESSED" is printed, and the program is terminated. If any key other than key A is pressed, the program goes round a loop, scanning the keyboard repeatedly.
The above program illustrates the typical situation the function KEY is used. It is most often used in an "IF-statement" located within a loop, such that the keyboard is scanned repeatedly, and certain action will be taken if a specified key is pressed. This enables the pressing of the specified key to control the flow of a program.
Note: the following information may become useful for a more detailed understanding of the KEY function. When a key is pressed, its ASCII code is sent to a register or latch. Functions like KEY (or INPUT) will read the content of this latch, and then clear the latch. If no key is pressed, KEY will return the value 0.
The built-in joystick is a device which controls 4 keys. It will input into the computer one of 4 codes according to which direction the joystick is pressed. The 4 codes (which will not be displayed) have ASCII decimal codes of 136, 137, 138, and 139, corresponding respectively to the up, right, down and left positions of the joystick. If the statement, "IF KEY = 136 THEN ..." appears somewhere within a loop, whenever the joystick is flicked to its "up" position, (which has an ASCII decimal code of 136), the condition within the IF statement is satisfied. This enables the joystick to control the flow of the program.