QBasic 1.1: KEY (Event Trapping) Statement


KEY(n%) ON
ON KEY(n%) GOSUB line

Description / Parameter(s)

n% A value that specifies a function key, direction key, or user-defined key:
n% Key
0 All keys listed here (KEY(0) ON, KEY(0) OFF, and KEY(0) STOP only).
1-10 Function keys F1-F10.
11 Up Arrow key.
12 Left Arrow key.
13 Right Arrow key.
14 Down Arrow key.
15-25 User-defined keys. For more information, see Declaring User-Defined Keys.
30, 31 Function keys F11 and F12.
KEY(n%) ON Enables event trapping for the specified key.
KEY(n%) OFF Disables key event trapping.
KEY(n%) STOP Suspends key event trapping. Events are processed once event trapping is enabled by KEY ON.
line The label or number of the first line of the event-trapping subroutine.


'This example requires Caps Lock and Num Lock to be off. CONST ESC = 27 KEY 15, CHR$(&H4) + CHR$(&H1F) 'Set up Ctrl+S as KEY 15. ON KEY(15) GOSUB PauseHandler KEY(15) ON WHILE INKEY$ <> CHR$(ESC) PRINT "Press Esc to stop, Ctrl+S to pause." PRINT WEND END PauseHandler: SLEEP 1 RETURN