Occurs while the object has focus and a key was pressed.
Object.OnKeyDown( nKeyCode, nShift )
The OnKeyDown event syntax has the following parts:
Name |
Type |
Description |
Object |
An object |
A control or the AForm object |
pnKeyCode |
Var Short, as enumAKey |
A key code, such as aKeyF1 (the F1 key), aKeyA (the A key), or aKeyHome (the HOME key). |
nShift |
Val enumAKeyShift |
An integer that corresponds to the state of the SHIFT, CTRL, and ALT keys when the button specified in the button argument is pressed or released. |
enumAKey can be one of the keys on the keyboard. See enumAKey for a list of the keys.
enumAKeyShift can be one of the following:
Name |
Value |
Description |
aKeyShiftNone |
0 |
No key is pressed. |
aKeyShiftShift |
1 |
SHIFT key is pressed. |
aKeyShiftCtrl |
2 |
CTRL key is pressed. |
aKeyShiftAlt |
4 |
ALT key is pressed. |
pnKeyCode is passed by reference, changing it sends a different character to the object, this applies only to non-printable characters: ESC, navigation keys, and ENTER. Changing pnKeyCode to 0 cancels the keystroke so that the object receives no character. To change to a printable character use the OnKeyPress event.
For both OnKeyUp and OnKeyDown
events, the object with the focus receives all keystrokes. A form can
have the focus only if it has no visible or enabled controls. Although
the OnKeyDown and OnKeyUp
events can apply to most keys, they're most often used for:
- Extended character keys such as function keys.
- Navigation key (arrow keys, home, end, page up/down).
- Combinations of keys with standard keyboard modifiers.
- Distinguishing between the numeric keypad and regular number keys.
Use OnKeyDown and OnKeyUp event procedures if you need to respond to both the pressing and releasing of a key.
OnKeyDown and OnKeyUp
are not invoked for:
- The ENTER key if the form has a AButton
control with the Default property set to True.
- The ESC key if the form has a AButton
control with the Cancel property set to True.
- The TAB key (except for the ATextBox
control with the WantReturn property
set to True).
OnKeyDown and OnKeyUp interpret the uppercase and lowercase of each character by means of two arguments: keycode, which indicates the physical key (thus returning A and a as the same key) and shift, which indicates the state of shift+key and therefore returns either A or a.
If you need to test for the shift arguments, you can use constants listed in the table above for enumAKeyShift.
The constants act as bit masks that you can use to test for any combination of keys.
You test for a condition by first assigning each result to a temporary integer variable and then comparing shift to a bit mask. Use the And operator with the shift argument to test whether the condition is < > 0, indicating that the modifier was pressed, as in this example:
bCtrlDown = (nShift And aKeyShiftCtrl) ! Test if the CTRL key was held down
In a procedure, you can test for any combination of conditions, as in this example:
If bShiftDown And bCtrlDown Then
Note: If the form's KeyPreview property is set to True, the form receives these events before controls on the form receive the events. Use the KeyPreview property to create global keyboard-handling routines.
chk1.OnKeyDown(pnKeyCode, nShift)
{
if pnKeyCode=aKeyLeft
pnKeyCode=aKeyRight
endif
}
|
||
|