KeyDown, KeyUp Events

       

Occur when the user presses (KeyDown) or releases (KeyUp) a key while an object has the focus. (To interpret ANSI characters, use the KeyPress event.)

Syntax

Private Sub Form_KeyDown(keycode As Integer, shift As Integer)

Private Sub object_KeyDown([index As Integer,]keycode As Integer, shift As Integer)

Private Sub Form_KeyUp(keycode As Integer, shift As Integer)

Private Sub object_KeyUp([index As Integer,]keycode As Integer, shift As Integer)

The KeyDown and KeyUp event syntaxes have these parts:

Part Description
object An object expression that evaluates to an object in the Applies To list.
index An integer that uniquely identifies a control if it's in a control array.
keycode A key code, such as vbKeyF1 (the F1 key) or vbKeyHome (the HOME key). To specify key codes, use the constants in the Visual Basic (VB) object library in the Object Browser.
shift An integer that corresponds to the state of the SHIFT, CTRL, and ALT keys at the time of the event. The shift argument is a bit field with the least-significant bits corresponding to the SHIFT key (bit 0), the CTRL key (bit 1), and the ALT key (bit 2 ). These bits correspond to the values 1, 2, and 4, respectively. Some, all, or none of the bits can be set, indicating that some, all, or none of the keys are pressed. For example, if both CTRL and ALT are pressed, the value of shift is 6.

Remarks

For both events, the object with the focus receives all keystrokes. A form can have the focus only if it has no visible and enabled controls. Although the KeyDown and KeyUp events can apply to most keys, they're most often used for:

Use KeyDown and KeyUp event procedures if you need to respond to both the pressing and releasing of a key.

KeyDown and KeyUp aren't invoked for:

KeyDown and KeyUp 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 argument, you can use the shift constants which define the bits within the argument. The constants have the following values:

Constant Value Description
vbShiftMask 1 SHIFT key bit mask.
VbCtrlMask 2 CTRL key bit mask.
VbAltMask 4 ALT key bit mask.

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 greater than 0, indicating that the modifier was pressed, as in this example:

ShiftDown = (Shift And vbShiftMask) > 0

In a procedure, you can test for any combination of conditions, as in this example:

If ShiftDown And CtrlDown Then

Note   If the KeyPreview property is set to True, a form receives these events before controls on the form receive the events. Use the KeyPreview property to create global keyboard-handling routines.