qbasic.hlp (Topic list)
Important Notice
The pages on this site contain documentation for very old MS-DOS software, purely for historical purposes. If you're looking for up-to-date documentation, particularly for programming, you should not rely on the information found here, as it will be woefully out of date.
KEY, ON KEY Statements (Event Trapping)
  Contents  Index  Back
──────────────────────────────────────────────────────────────────────────────
KEY enables, disables, or suspends event trapping of a key.
If event trapping is enabled, ON KEY branches to a subroutine whenever
the key is pressed.
 
KEY(n%) ON
KEY(n%) OFF
KEY(n%) STOP
ON KEY(n%) GOSUB line
 
    ■ 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.
 
Example:
    '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
 
See Also    KEY (Assignment)    Declaring User-Defined Keys