KeyDown
PowerScript function
Description
Determines whether the user pressed the specified key on the
computer keyboard.
Syntax
|
1 |
KeyDown ( keycode ) |
|
Argument |
Description |
|---|---|
|
keycode |
A value of the KeyCode enumerated datatype that identifies |
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 KeyMiddleButton! — Middle mouse KeyRightButton! — Right mouse |
|
Letters |
KeyA! – KeyZ! — A – Z, uppercase or |
|
Other symbols |
KeyQuote! — ‘ and “ KeyEqual! — = and KeyComma! — , and < KeyDash! — – KeyPeriod! — . and > KeySlash! KeyBackQuote! — ` and KeyLeftBracket! — [ and { KeyBackSlash! KeyRightBracket! — ] and KeySemiColon! — ; and: |
|
Non-printing characters |
KeyBack! — KeyTab! KeyEnter! KeySpaceBar! |
|
Function keys |
KeyF1! – KeyF12! — Function keys F1 to |
|
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 symbols |
KeyMultiply! — * on numeric KeyAdd! — + on numeric KeySubtract! — – on numeric KeyDecimal! — . on numeric KeyDivide! — / on numeric 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:
|
1 2 3 4 5 |
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:
|
1 2 3 4 |
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):
|
1 |
IF KeyDown(65) THEN ... |
This statement tests whether the user pressed the Shift key and the
A key:
|
1 |
IF KeyDown(65) AND KeyDown(KeyShift!) THEN ... |
This statement in a Clicked event checks whether the Shift is also
pressed:
|
1 |
IF KeyDown(KeyShift!) THEN ... |