Q(uick)BASIC Statements (Event Trapping): KEY

Quick View

KEY

Event-trapping statements that start or stops trapping of specified keys

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
  • KEY(n) ON
  • KEY(n) OFF
  • KEY(n) STOP
Description/Parameter(s)

The argument n is the number of a FUNCTION key, a cursor-direction key, or a user-defined key. See the KEY statements for information on assigning soft-key values to FUNCTION keys. The values of n are as follows:

Value Key
1-10 The FUNCTION keys F1-F10
11 UP
12 LEFT
13 RIGHT
14 DOWN
15-25 User-defined keys
30-31 The FUNCTION keys F11-F12 on 101-key keyboards

LEFT, RIGHT, UP, and DOWN refer to the direction keys.

You can enable trapping of combination keys by using a variation of the KEY statement:

  • KEY n, CHR$(keyboardflag) + CHR$(scancode)

The argument n is in the range 15-25 to indicate a user-defined key. The keyboardflag can be any combination of the following hexadecimal values:

Value Key
&H00 No keyboard flag
&H01-&H03 Either SHIFT key
&H04 CTRL
&H08 ALT
&H20 NUMLOCK
&H40 CAPSLOCK
&H80 101-key keyboard extended keys

You can add the values together to test for multiple shift states. A keyboardflag value of &H12 would test for both CTRL and ALT being pressed, for example.

Because key trapping assumes the left and right SHIFT keys are the same, you can use either &H01, &H02, or &H03 to indicate a SHIFT key. The scancode argument is a number identifying one of the 83 keys to trap, as shown in the Keyboard Scan Codes .

The KEY(n) ON statement enables soft-key or cursor-direction-key event trapping by an ON KEY statement. If you specify a nonzero line number in the ON KEY statement while trapping is enabled, BASIC checks to see if you have pressed KEY(n). If you have, BASIC executes the GOSUB clause in the ON KEY statement. The text that would normally be associated with a FUNCTION key is not input.

When you are working in the environment, QuickBASIC tests between statements for key presses. In stand-alone programs, you can specify checking between lines.

KEY(n) OFF disables the event trap; even if an event takes place, it is not remembered. KEY(n) STOP inhibits the event trap; that is, if you press the specified key your action is remembered and an ON KEY event trap is executed as soon as a KEY(n) ON statement is executed.

Example

This example traps the DOWN direction key and CTRL+S (control key and lowercase "s"). To trap the combination of the CTRL key and uppercase "s", trap CTRL+SHIFT and CTRL+CAPS-LOCK+s.
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$(&H04) + 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
Syntax
  • KEY(n%) ON
  • KEY(n%) OFF
  • KEY(n%) STOP
Description/Parameter(s)
n% Key
0 All keys listed in this table
1-10 F1-F10
11 Up Arrow key
12 Left Arrow key
13 Right Arrow key
14 Down Arrow key
15-25 User-defined keys
30-31 F11-F12 on 101-key keyboards

Usage Notes

  • KEY(n%) ON enables trapping of function keys, direction keys, and user-defined keys. If key n% is pressed after a KEY(n%) ON statement, the routine specified in the ON KEY statement is executed.
  • KEY(n%) OFF disables trapping of key n%. No trapping takes place until another KEY(n%) ON statement is executed. Events occurring while trapping is off are ignored.
  • KEY(n%) STOP suspends trapping of key n%. No trapping takes place until a KEY(n%) ON statement is executed. Events occurring while trapping is suspended are remembered and processed when the next KEY(n%) ON statement is executed. However, remembered events are lost if KEY(n%) OFF is executed.
  • When a key event trap occurs (that is, the GOSUB is performed), an automatic KEY STOP is executed so that recursive traps cannot take place. The RETURN from the trapping routine automatically executes a KEY ON statement unless an explicit KEY OFF was performed inside the routine.
  • 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."
  • For more information, see Chapter 9, "Event Handling" in the Programmer's Guide.

User-Defined Keys

  • In addition to providing the preassigned key numbers 1-14 (plus 30 and 31 with the 101-key keyboard), BASIC enables you to create user- defined keys. You do this by assigning the numbers 15-25 to any of the remaining keys on the keyboard. Use KEY (Assignment) to create user-defined keys.
  • You can also set a trap for "shifted" keys. A key is shifted when you press it simultaneously with one or more of the special keys Shift, Ctrl, or Alt, or if you press it after pressing NumLock or Caps Lock. Use the KEY statements (assignment) to define shifted keys before you can trap them. The syntax for KEY (Assignment) is shown below:

  • KEY n%, CHR$(keyboardflag) + CHR$(scancode)
n% Is between 15 and 25 and indicates a user-defined key.
keyboardflag Can be any combination of the values in the Keyboard Flag Table below:
ValueKey
0No keyboard flag
1,2 or 3Either Shift key
4Ctrl
8Alt
32NumLock
64Caps Lock
128Keys on the 101-key keyboard that are duplicates of keys on smaller keyboards (such as Alt and Del).
scancode A number that identifyies one of 83 keys to trap. See the Scan Code Table
  • You can add the values in the Keyboard Flag Table together to test for multiple shift states. A keyboardflag value of 12 would test for both Ctrl and Alt being pressed, for example.
  • To define Shift, Ctrl, Alt, NumLock, or Caps Lock as a user-defined key (by itself, not in combination with another key), use a keyboard flag of 0. For example, to define Alt as a user-defined key:

  • KEY CHR$(0) + CHR$(56)

  • To define Alt + Alt as a user-defined key (the second Alt will be trapped as it is being pressed only if the first Alt key is already pressed):

  • KEY CHR$(8) + CHR$(56)

  • Because key trapping assumes the left and right Shift keys are the same, you can use either 1, 2, or 3 to indicate a Shift key. The scancode argument is a number identifying one of the 83 keys to trap, as shown in the Scan Code Table .
  • The scan codes in the Scan Code Table are equivalent to the first column of the scan code table in Appendix A, "Keyboard Codes and ASCII Character Codes" in the BASIC Language Reference. The codes in the other columns of the table in the appendix should not be used for key trapping.
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