KeyDown

Description

Determines whether the user pressed the specified key on the computer keyboard.

Syntax

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.


Return value

Boolean.

Returns true if keycode was pressed and false if it was not. If keycode is null, KeyDown returns null.

Usage

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.

The DataWindow columns with RichText edit style does not support the KeyDown event.

Events

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.

KeyCodes and ASCII values

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

KeyLeftButton! -- Left mouse button

KeyMiddleButton! -- Middle mouse button

KeyRightButton! -- Right mouse button

Letters

KeyA! - KeyZ! -- A - Z, uppercase or lowercase

Other symbols

KeyQuote! -- ' and "

KeyEqual! -- = and +

KeyComma! -- , and <

KeyDash! -- - and _

KeyPeriod! -- . and >

KeySlash! -- / and ?

KeyBackQuote! -- ` and ~

KeyLeftBracket! -- [ and {

KeyBackSlash! -- \ and |

KeyRightBracket! -- ] and }

KeySemiColon! -- ; and:

Non-printing characters

KeyBack! -- Backspace

KeyTab!

KeyEnter!

KeySpaceBar!

Function keys

KeyF1! - KeyF12! -- Function keys F1 to F12

Control keys

KeyShift!

KeyControl!

KeyAlt!

KeyPause!

KeyCapsLock!

KeyEscape!

KeyPrintScreen!

KeyInsert!

KeyDelete!

Navigation keys

KeyPageUp!

KeyPageDown!

KeyEnd!

KeyHome!

KeyLeftArrow!

KeyUpArrow!

KeyRightArrow!

KeyDownArrow!

Numeric and symbol keys

Key0! -- 0 and )

Key1! -- 1 and !

Key2! -- 2 and @

Key3! -- 3 and #

Key4! -- 4 and $

Key5! -- 5 and %

Key6! -- 6 and ^

Key7! -- 7 and &

Key8! -- 8 and *

Key9! -- 9 and (

Keypad numbers

KeyNumpad0! - KeyNumpad9! 0 - 9 on numeric keypad

Keypad symbols

KeyMultiply! -- * on numeric keypad

KeyAdd! -- + on numeric keypad

KeySubtract! -- - on numeric keypad

KeyDecimal! -- . on numeric keypad

KeyDivide! -- / on numeric keypad

KeyNumLock!

KeyScrollLock!


Examples

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 ...