To create an event procedure that is executed when the KeyDown or KeyUp event occurs, set the OnKeyDown or OnKeyUp property to [Event Procedure], and click the Build button.
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 use the following arguments.
Argument |
Description |
controlname |
A string that is the name of the control affected by the KeyDown and KeyUp event procedures. |
KeyCode |
A key code, such as vbKeyF1 (the F1 key) and vbKeyHome (the HOME key). To specify key codes, use the intrinsic constants 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 Bit mask for the SHIFT key. acCtrlMask Bit mask for the CTRL key. acAltMask Bit mask for the ALT key. |
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) > 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. . .
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 KeyPreview 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.
KeyDown, KeyUp Events — Macros.
The following example determines whether you have pressed the SHIFT, CTRL, or ALT key.
To try this example, add the following code to the Declarations section of a module for 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."Sub