Determines whether the user pressed the specified key on the computer keyboard.
KeyDown ( keycode )
Argument |
Description |
---|---|
keycode |
A value of the KeyCode enumerated datatype that identifies a key on the computer keyboard or an integer whose value is the ASCII code for a key. Not all ASCII values are recognized; see Usage. See also the table of KeyCode values in Usage. |
Boolean. Returns true if keycode was pressed and false if it was not. If keycode is null, KeyDown returns null.
KeyDown does not report what character the user typed—it reports whether the user was pressing the specified key when the event whose script is calling KeyDown was triggered.
You can call KeyDown in a window’s Key event or a keypress event for a control to determine whether the user pressed a particular key. The Key event occurs whenever the user presses a key as long as the insertion point is not in a line edit. The Key event is triggered repeatedly if the user holds down a repeating key. For controls, you can define a user event for pbm_keydown or pbm_dwnkey (DataWindows), and call KeyDown in its script.
You can also call KeyDown in a mouse event, such as Clicked, to determine whether the user also pressed a modifier key, such as Ctrl.
KeyDown does not distinguish between uppercase and lowercase letters or other characters and their shifted counterparts. For example, KeyA! refers to the A key—the user may have typed "A" or "a." Key9! refers to both "9" and "(". Instead, you can test whether a modifier key is also pressed.
KeyDown does not test whether Caps Lock or other toggle keys are in a toggled-on state, only whether the user is pressing it.
KeyDown only detects ASCII values 65-90 (KeyA! - KeyZ!) and 48-57 (Key0!-Key9!). These ASCII values detect whether the key was pressed, whether or not the user also pressed Shift or Caps Lock. KeyDown does not detect other ASCII values (such as 97-122 for lowercase letters).
The following table categorizes KeyCode values by type of key and provides explanations of names that might not be obvious.
Type of key |
KeyCode values and descriptions |
---|---|
Mouse buttons |
|
Letters |
KeyA! - KeyZ! A - Z, uppercase or lowercase |
Other symbols |
|
Non-printing characters |
|
Function keys |
KeyF1! - KeyF12! Function keys F1 to F12 |
Control keys |
|
Navigation keys |
|
Numeric and symbol keys |
|
Keypad numbers |
KeyNumpad0! - KeyNumpad9! 0 - 9 on numeric keypad |
Keypad symbols |
|
The following code checks whether the user pressed the F1 key or the Ctrl key and executes some statements appropriate to the key pressed:
IF KeyDown(KeyF1!) THEN
. . . // Statements for the F1 key
ELSEIF KeyDown(KeyControl!) THEN
. . . // Statements for the CTRL key
END IF
This statement tests whether the user pressed Tab, Enter, or any of the scrolling keys:
IF (KeyDown(KeyTab!) OR KeyDown(KeyEnter!) OR &
KeyDown(KeyDownArrow!) OR KeyDown(KeyUpArrow!) &
OR KeyDown(KeyPageDown!) OR KeyDown(KeyPageUp!))&
THEN ...
This statement tests whether the user pressed the A key (ASCII value 65):
IF KeyDown(65) THEN ...
This statement tests whether the user pressed the Shift key and the A key:
IF KeyDown(65) AND KeyDown(KeyShift!) THEN ...
This statement in a Clicked event checks whether the Shift is also pressed:
IF KeyDown(KeyShift!) THEN ...