KeyDown Event
Object List Next Object
Defined By: Form
Description:
Occurs on the downstroke when the user presses a key while an object has the
keyboard focus and is sent repeatedly as long as the key is held down. It is
commonly used in combination with the KeyUp event to process the press and/or
release of any key (compare to the KeyPress event, which receives only ANSI
characters).
Usage:
Sub KeyDown (keyCode As Integer, ByVal shift As Integer)
Syntax Description
KeyCode Provides a unique number identifier, or key code, for the key pressed. The key
code is the virtual-key value as defined by the Win32 API. Constants are
provided with Phoenix to make key event handling code more readable, e.g. KEY_DELETE
(Del key) and KEY_F2 (F2 key). Key code constants are defined in the “KEYCODES” module.
Shift Indicates the status of the SHIFT, CTRL, and ALT keys at the time the event
was called. Each of these keys has a corresponding bit-flag value. SHIFT has a
value of 1, CTRL is 2, and ALT is 4. To indicate which key or keys were pressed,
the key values are summed in the Shift argument. For example, if both CTRL and ALT are pressed, the value of Shift is 6.
Details:
The object with the keyboard focus receives all keystrokes. Generally, this
object is a control. A form receives key events only if it has no active controls
or if the form's KeyPreview property is set to True. When a form's KeyPreview
property is set to True, the form receives key events before the controls on
the form, and can modify the key that will be seen by the control when it
receives the event. This is useful for creating form-specific keyboard-handling
routines, such as special shortcuts or accelerators.
The KeyDown event can process all keys, not just character keys. However, it
is most commonly used to distinguish between the numeric keypad and regular
numbers or to react to users pressing:
Extended character keys such as function keys.
Navigation keys such as arrow keys.
Key combinations such as CTRL-SHIFT-key.
To test for the Shift argument, you can declare constants that define the bits
within the argument. The Shift constants have the following values:
Constant Value Bit
SHIFT_MASK 1 0
CTRL_MASK 2 1
ALT_MASK 4 2
You can use these constants to test for any combination of modifiers.
Note: To interpret ANSI characters, use the KeyPress event.
Pressing and holding a key down activates this event repeatedly until the key
is released.