See Also Example
Apply To
Form, check box
Description
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
Sub Form_KeyDown (KeyCode As Integer, Shift As Integer)
Sub ctlname_KeyDown ([Index As Integer,]KeyCode As Integer, Shift As Integer)
Sub Form_KeyUp (KeyCode As Integer, Shift As Integer)
Sub ctlname_KeyUp ([Index As Integer,]KeyCode As Integer, Shift As Integer)
Remarks
The KeyDown and KeyUp events use these arguments:
Argument Description
Index Uniquely identifies a control if it is in a control array .
KeyCode A key code, such as, KEY_F1 (F1 key) and KEY_HOME (Home key). To specify key codes, use the constants in CONSTANT.TXT, a Visual Basic file that specifies system defaults.
Shift 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 is pressed. For example, if both Ctrl and Alt are pressed, the value of Shift is 6.
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 KeyDown and KeyUp can apply to most keys, they are most often used for:
Extended character keys such as function keys .
Navigation keys .
Combinations of keys with standard keyboard modifiers .
Distinguishing between the numeric keypad and regular number keys.
Use KeyDown and KeyUp event procedures for keyboard handlers if you need to respond to both the pressing and releasing of a key.
KeyDown and KeyUp are not invoked for:
The Enter key if the form has a command button with the Default property set to True.
The Esc key if the form has a command button with the Cancel property set to True.
The Tab key.
If you need to test for the Shift argument, you can declare constants The Shift constants have the following values:
Constant Value
SHIFT_MASK 1
CTRL_MASK 2
ALT_MASK 4
The constants act as bit masks You can place the constants at the procedure level or in the Declarations section of a module where you can use this syntax:
Global Const Constantname = expression
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 pressedfor example:
ShiftDown = (Shift And SHIFT_MASK) > 0
In a procedure, you can test for any combination of conditionsfor example:
If ShiftDown And CtrlDown Then
Note If the KeyPreview property is True, a form receives these events before controls on the form receive the events. Use the KeyPreview property to create global keyboard-handling routines.
Help:
Keyboard
KeyPreview Property
Operator Precedence
SendKeys Statement
Programmer's Guide:
Chapter 3, "Creating and Using Controls"
The example demonstrates a generic keyboard handler that responds to the F2 function key and to all the associated Alt, Shift, and Ctrl combinations. To find the value of the key constants, look at the CONSTANT.TXT file or load it into a module. To try this example, paste the code into the Declarations section of a form that contains a text box. Then press F5 and press F2 with the Alt Shift, and Ctrl keys.
Sub Text1_KeyDown (KeyCode As Integer, Shift As Integer)
Dim ShiftDown, AltDown, CtrlDown, Txt
Const KEY_F2 = &H71 ' Define constants.
Const SHIFT_MASK = 1
Const CTRL_MASK = 2
Const ALT_MASK = 4
ShiftDown = (Shift And SHIFT_MASK) > 0
AltDown = (Shift And ALT_MASK) > 0
CtrlDown = (Shift And CTRL_MASK) > 0
If KeyCode = KEY_F2 Then ' Display key combinations.
If ShiftDown And CtrlDown And AltDown Then
Txt = "Shift + Ctrl + Alt F2."
ElseIf ShiftDown And AltDown Then
Txt = "Shift + Alt F2."
ElseIf ShiftDown And CtrlDown Then
Txt = "Shift + Ctrl + F2."
ElseIf CtrlDown And AltDown Then
Txt = "Ctl + Alt + F2."
ElseIf ShiftDown Then
Txt = "Shift F2."
ElseIf CtrlDown Then
Txt = "Ctrl F2."
ElseIf AltDown Then
Txt = "Alt F2."
ElseIf Shift = 0 Then
Txt = "F2."
End If
Text1.Text = "You pressed " & Txt
End If
End Sub