KeyDown PowerScript function
Description
Determines whether the user pressed the specified key on the
computer keyboard.
Syntax
|
1 |
<span>KeyDown</span> ( <span>keycode</span> ) |
|
Argument |
Description |
|---|---|
|
keycode |
A value of the KeyCode enumerated datatype |
Return Values
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.
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 |
|
|
Letters |
KeyA! |
|
Other symbols |
|
|
Non-printing characters |
|
|
Function keys |
KeyF1! |
|
Control keys |
|
|
Navigation keys |
|
|
Numeric and symbol keys |
|
|
Keypad numbers |
KeyNumpad0! |
|
Keypad symbols |
|
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 |
IF <span>KeyDown</span>(KeyF1!) THEN |
|
1 |
. . . // Statements for the F1 key |
|
1 |
ELSEIF <span>KeyDown</span>(KeyControl!) THEN |
|
1 |
. . . // Statements for the CTRL key |
|
1 |
END IF |
This statement tests whether the user pressed Tab,
Enter, or any of the scrolling keys:
|
1 |
IF (<span>KeyDown</span>(KeyTab!) OR <span>KeyDown</span>(KeyEnter!) OR & |
|
1 |
<span>KeyDown</span>(KeyDownArrow!) OR <span>KeyDown</span>(KeyUpArrow!) & |
|
1 |
OR <span>KeyDown</span>(KeyPageDown!) OR <span>KeyDown</span>(KeyPageUp!))& |
|
1 |
THEN ... |
This statement tests whether the user pressed the
A key (ASCII value 65):
|
1 |
IF <span>KeyDown</span>(65) THEN ... |
This statement tests whether the user pressed the
Shift key and the A key:
|
1 |
IF <span>KeyDown</span>(65) AND <span>KeyDown</span>(KeyShift!) THEN ... |
This statement in a Clicked event checks whether
the Shift is also pressed:
|
1 |
IF <span>KeyDown</span>(KeyShift!) THEN ... |