Q(uick)BASIC Statement: ON KEY

Quick View

ON KEY(n)

Event trapping statement that specifies a subroutine to branch to when key n is pressed

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • KEY(n%) ON
  • KEY(n%) OFF
  • KEY(n%) STOP
  • ON KEY(n%) GOSUB line
Description/Parameter(s)
n% A value that specifies a function key, direction key, or user-defined key:
n%Key
0All keys listed here (KEY(0) ON, KEY(0) OFF, and KEY(0) STOP only).
1-10Function keys F1-F10.
11Up Arrow key.
12Left Arrow key.
13Right Arrow key.
14Down Arrow key.
15-25User-defined keys. For more information, see Declaring User-Defined Keys below.
30, 31Function 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.

Declaring User-Defined Keys

  • To declare a user-defined key, use the following variation of the KEY statement:

  • KEY n%, CHR$(keyboardflag%) + CHR$(scancode%)
n% A value in the range 15 through 25 that identifies the key.
keyboardflag% One of the following values, or a sum of values, specifying whether the user-defined key is used in combination with the Shift, Ctrl, Alt, NumLock, or Caps Lock keys, or with extended keys:
ValueKey
0No keyboard flag
1 through 3Either Shift key
4Ctrl key
8Alt key
32NumLock key
64Caps Lock key
128Extended keys on a 101-key keyboard
To specify multiple shift states, add the values together. For example, a value of 12 specifies that the user-defined key is used in combination with the Ctrl and Alt keys.
scancode% The scan code for the key being declared. See Keyboard Scan Codes .
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
Syntax
  • ON KEY(n) GOSUB {linelabel | linenumber}
Description/Parameter(s)
  • n, an integer expression, is the number of a function key, direction key, or user-defined key
  • linelabel or linenumber is the first line of the event-handling subroutine

  • Keys are processed in the following order:
    1. The line printer's echo-toggle key is processed first. Defining this key as a user-defined key trap does not prevent characters from being echoed to the line printer when pressed.
    2. Function keys and the cursor-direction keys are examined next. Defining a FUNCTION key or DIRECTION key as a user-defined key trap has no effect because these keys are predefined.
  • The ON KEY statement can trap any key, including BREAK or system reset. This makes it possible to prevent accidentally breaking out of a program or rebooting the machine.
Note: When a key is trapped, the key event is destroyed. You cannot subsequently use INPUT or INKEY$ statements to find out which key caused the trap. Because there is no way to know which key press caused the branch to the trap, you must set up a subroutine for each key if you want to assign different functions to particular keys.
Example

See the Selected Event-Trapping Programming Example 1 , which uses the ON KEY(n) statement.

Syntax
  • ON KEY(n%) GOSUB {linenumber | linelabel}
Description/Parameter(s)
n% An integer expression; the number of a function key, direction key, or user-defined key.
linenumber or linelabel The first line of the event-handling routine to branch to when a function key, direction key, or user-defined key is pressed.

Usage Notes

  • Keys are processed in the following order:
    1. The line printer's echo-toggle key is processed first. Defining this key as a user-defined key trap does not prevent characters from being echoed to the line printer when pressed.
    2. Function keys and the direction keys are examined next. Defining a function key or direction key as a user-defined key trap has no effect because these keys are predefined.
    3. Finally, the user-defined keys are examined.
  • The ON KEY statement can trap any key, including Break or system reset (Ctrl+Alt+Del). This makes it possible to prevent accidentally breaking out of a program or rebooting the machine.
  • The ON KEY statement specifies only the start of an event-trapping routine. The KEY Statements determine whether the routine is called and how events are handled when trapping is off:
    KEY ONEnables event trapping. Event trapping occurs only after a KEY ON statement is executed.
    KEY OFFDisables event trapping. Even if an event takes place, it is not remembered.
    KEY STOPSuspends event trapping. Any events that occur are remembered and are trapped as soon as a KEY ON statement is executed.
  • If your program contains event-handling statements and you are compiling from the BC command line, use the BC /W or /V option. (The /W option checks for events at every label or line number; the /V option checks at every statement.) If you do not use these options and your program contains event traps, BASIC generates the error message, "ON event without /V or /W on command line."
  • The RETURN linenumber or RETURN linelabel forms of RETURN can be used to return to a specific line from the trapping routine. Use this type of return with care, however, because any GOSUB, WHILE, or FOR statements active at the time of the trap remain active. BASIC may generate error messages such as "NEXT without FOR." In addition, if an event occurs in a procedure, a RETURN linenumber or RETURN linelabel statement cannot get back into the procedure because the line number or label must be in the module-level code.

Important

  • After a key is trapped, the information on which key was trapped is no longer available. This means that you cannot subsequently use an INPUT statement or INKEY$ function to find out which key caused the trap. Because you do not know which key press caused the trap, you must create a different event-handling routine for each key you want to trap.
Example

This example uses the KEY and ON KEY statements to trap the CTRL+S (control key and lowercase "s") and the down direction key.
Note: Do not run this example with the NumLock key depressed.

I = 0 CLS 'Clear screen. PRINT "Press DOWN direction key to end." KEY 15, CHR$(&H4) + CHR$(&H1F) KEY(15) ON 'Trap CTRL+s. KEY(14) ON 'Trap DOWN direction key. ON KEY(15) GOSUB Keytrap ON KEY(14) GOSUB Endprog Idle: GOTO Idle 'Endless loop. Keytrap: 'Counts the number of times CTRL+s pressed. I = I + 1 RETURN Endprog: PRINT "CTRL+s trapped"; I; "times" END RETURN