The ON KEY(n) statement defines a line number or label to go to when a specified key is pressed.
- Predefined key numbers are shown below, where n is the number that represents that key.
- 1 - 10 for F1 - F10 function keys respectively
- 11, 12, 13 and 14 for UP, LEFT, RIGHT and DOWN numberpad arrows respectively.
- 15-25(29 in QB64) for user-defined keys. See: KEY n, Keyboard scancodes
- 30 - 31 for F11 - F12 function keys
Example 1: Using ON KEY with GOSUB to execute code.
Example 2: Using ON KEY with a SUB procedure to do something.
CLS ON KEY(10) Control 'action of user defined key press KEY(10) ON 'turn ON event trapping for key combination PRINT "Press F10 key, escape quits!" DO: SLEEP count = count + 1 PRINT count; IF INKEY$ = CHR$(27) THEN END 'escape key exit LOOP SUB Control PRINT "F10 key pressed!"; BEEP END SUB