Description
To create an event procedure that runs when the KeyDown or KeyUp event occurs, set the OnKeyDown or OnKeyUp property to [Event Procedure], and click the Build button.
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.| Argument | Description | |
| controlname | The name of the control whose KeyUp or KeyDown event procedure you want to run. | |
| KeyCode | A 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. | |
| Shift | The 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: | |
| Constant | Description | |
| acShiftMask | The bit mask for the SHIFT key. | |
| acCtrlMask | The bit mask for the CTRL key. | |
| acAltMask | The 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 And 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) > 0If ShiftDown And CtrlDown Then
.            ' Do this if SHIFT and CTRL keys are pressed.
.
.
End IfSee Also KeyDown, KeyUp events — macros.
Example The following example determines whether you have pressed the SHIFT, CTRL, or ALT key. To try the example, add the following event procedure to a form containing a text box named KeyHandler.Private Sub KeyHandler_KeyDown(KeyCode As Integer, Shift As Integer)
    Dim intShiftDown As Integer, intAltDown As Integer
    Dim intCtrlDown As Integer
    ' Use bit masks to determine which key was pressed.
    intShiftDown = (Shift And acShiftMask) > 0
    intAltDown = (Shift And acAltMask) > 0
    intCtrlDown = (Shift And acCtrlMask) > 0
    ' Display message telling user which key was pressed.
    If intShiftDown Then MsgBox "You pressed the SHIFT key."
    If intAltDown Then MsgBox "You pressed the ALT key."
    If intCtrlDown Then MsgBox "You pressed the CTRL key."
End Sub