KeyDown, KeyUp EventsĀ  Event Procedures

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

         aa200370(v=office.10).md

To create an event procedure that runs when the KeyDown or KeyUp event occurs, set the property to [Event Procedure], and click the Build button aa220022(v=office.10).md.

Syntax

**Private Sub Form_KeyDown(**KeyCode As Integer, Shift As Integer)

Private Sub controlname**_KeyDown(**KeyCode As Integer, Shift As Integer)

**Private Sub Form_KeyUp(**KeyCode As Integer, Shift As Integer)

Private Sub controlname**_KeyUp(**KeyCode As Integer, Shift As Integer)

The KeyDown and KeyUp events have the following arguments.

ArgumentDescription
controlnameThe name of the control whose KeyUp or KeyDown event procedure you want to run.
KeyCodeA key code, such as vbKeyF1 (the F1 key) or vbKeyHome (the HOME key). To specify key codes, use the intrinsic constants shown in the Object Browser. You can prevent an object from receiving a keystroke by setting KeyCode to 0.
ShiftThe state of the SHIFT, CTRL, and ALT keys at the time of the event. If you need to test for the Shift argument, you can use one of the following intrinsic constants as bit masks:
ConstantDescription
 acShiftMaskThe bit mask for the SHIFT key.
 acCtrlMaskThe bit mask for the CTRL key.
 acAltMaskThe bit mask for the ALT key.

Remarks

You test for a condition by first assigning each result to a temporary integer variable and then comparing the Shift argument to an intrinsic constant. Use the operator with the Shift argument to test whether the condition is greater than 0, indicating that the SHIFT, CTRL, or ALT key was pressed, as in the following example:

  ShiftDown = (Shift And acShiftMask) > 0

In an event procedure, you can test for any combination of conditions, as in the following example:

  If ShiftDown And CtrlDown Then
.    ' Do this if SHIFT and CTRL keys are pressed.
.
.
End If

You can use the KeyDown and KeyUp event procedures to interpret the uppercase and lowercase version of each character by testing for both the KeyCode argument and the Shift argument. The KeyCode argument indicates the physical key pressed (thus, A and a are considered the same key), and the Shift argument indicates the state of SHIFT+key and returns either A or a.

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

You can respond to specific keys pressed in a form, regardless of which control has the focus. For example, you may want the key combination CTRL+X to always perform the same action on a form. To make sure a form receives all keyboard events, even those that occur for controls, before they occur for the controls, set the property of the form to Yes. With this property setting, all keyboard events occur first for the form, and then for the control that has the focus. You can respond to specific keystrokes in the form's KeyDown, KeyPress and KeyUp events. You can prevent a control from receiving keystrokes you've responded to, and prevent the keyboard events from occurring for the control, by setting the KeyCode argument to 0 for both the KeyDown and KeyUp events, and setting the KeyAscii argument to 0 for the KeyPress event (if the key is an ANSI key). You must set all three arguments to 0 if you don't want the control to receive the keystrokes.

You can use the arguments for the KeyDown, KeyPress, and KeyUp events, in conjunction with the arguments for the MouseDown, MouseUp, and MouseMove events, to make your application work smoothly for both keyboard and mouse users.

You can't cancel the KeyDown or KeyUp event.