QB64.com

QB64 is a modern extended BASIC programming language that retains QBasic/QuickBASIC 4.5 compatibility and compiles native binaries for Windows, Linux, and macOS.

The ON KEY(n) statement defines a line number or label to go to (or a SUB to run) when a specified key is pressed.

Syntax

ON KEY(n) GOSUB linelabel|linenumber ON KEY(n) SUBprocedure

Description

Example(s)

Using ON KEY with GOSUB to execute code.


KEY(1) ON
ON KEY(1) GOSUB trap
PRINT "Press F1 to quit!"
DO:LOOP          'never ending loop

trap:
PRINT "You pressed F1 like I told you to :)"
END
RETURN 

Setting multiple ON KEY statements to send different values to a SUB procedure.

  
FOR n = 1 TO 10
  KEY n, STR$(n)  '   assigns soft key as a numerical string 
  ON KEY(n) Funct n  'designate SUB procedure and parameter value passed
  KEY(n) ON '         turns each key event monitor on
NEXT
KEY ON  'displays F1 to F10 soft key assignments at bottom of screen

DO
LOOP UNTIL INKEY$ = CHR$(27)
END

SUB Funct (num%)
CLS'                  clears the screen and refreshes bottom soft key list
PRINT "You pressed F"; LTRIM$(STR$(num%))
END SUB 

See Also