Visual Basic Concepts
The KeyUp and KeyDown events report the exact physical state of the keyboard itself: A key is pressed down (KeyDown) and a key is released (KeyUp). In contrast, the KeyPress event does not report the state of the keyboard directly — it doesn't recognize the up or down state of the key, it simply supplies the character that the key represents.
A further example helps to illustrate the difference. When the user types uppercase "A," the KeyDown event gets the ASCII code for "A." The KeyDown event gets the same code when the user types lowercase "a." To determine whether the character pressed is uppercase or lowercase, these events use the shift argument. In contrast, the KeyPress event treats the uppercase and lowercase forms of a letter as two separate ASCII characters.
The KeyDown and KeyUp events return information on the character typed by providing the following two arguments.
Argument | Description |
Keycode | Indicates the physical key pressed. In this case, "A" and "a" are returned as the same key. They have the identical keycode value. But note that "1" on the typewriter keys and "1" on the numeric keypad are returned as different keys, even though they generate the same character. |
Shift | Indicates the state of the SHIFT, CTRL, and ALT keys. Only by examining this argument can you determine whether an uppercase or lowercase letter was typed. |
The keycode argument identifies a key by the ASCII value or by the key-code constant. Key codes for letter keys are the same as the ASCII codes of the uppercase character of the letter. So the keycode for both "A" and "a" is the value returned by Asc("A"). The following example uses the KeyDown event to determine if the "A" key has been pressed:
Private Sub Text1_KeyDown(KeyCode As Integer, _
Shift As Integer)
If KeyCode = vbKeyA Then MsgBox "You pressed _
the A key."
End Sub
Pressing SHIFT + "A" or "A" without the SHIFT key displays the message box — that is, the argument is true in each case. To determine if the uppercase or lowercase form of the letter has been pressed you need to use the shift argument. See the topic, "The Shift Argument" later in this chapter.
Key codes for the number and punctuation keys are the same as the ASCII code of the number on the key. So the keycode for both "1" and "!" is the value returned by Asc("1"). Again, to test for the "!" character you need to use the shift argument.
The KeyDown and KeyUp events can recognize most of the control keys on a standard keyboard. This includes the function keys (F1-F16), the editing keys (HOME, PAGE UP, DELETE, etc.), the navigation keys (RIGHT, LEFT, UP, and DOWN ARROW), and the keypad. These keys can be tested for by using either the key-code constant or the equivalent ASCII value. For example:
Private Sub Text1_KeyDown(KeyCode As Integer, _
Shift As Integer)
If KeyCode = vbKeyHome Then MsgBox "You _
pressed the HOME key."
End Sub
For More Information For a complete list of character codes, see "Character Set (0–127)" and "Character Set (128–255)" in the Language Reference. A complete list of key code constants with corresponding ASCII values is available in "Key Code Constants" or by using the Object Browser and searching for KeyCodeConstants.
The key events use the shift argument in the same way that the mouse events do — as integer and constant values that represent the SHIFT, CTRL, and ALT keys. You can use the shift argument with KeyDown and KeyUp events to distinguish between uppercase and lowercase characters, or to test for the various mouse states.
Building on the previous example, you can use the shift argument to determine whether the uppercase form of a letter is pressed.
Private Sub Text1_KeyDown(KeyCode As Integer, _
Shift As Integer)
If KeyCode = vbKeyA And Shift = 1 _
Then MsgBox "You pressed the uppercase A key."
End Sub
Like the mouse events, the KeyUp and KeyDown events can detect the SHIFT, CTRL, and ALT individually or as combinations. The following example tests for specific shift-key states.
Open a new project and add the variable ShiftKey
to the Declarations section of the form:
Dim ShiftKey as Integer
Add a Textbox control to the form and this procedure in the KeyDown event:
Private Sub Text1_KeyDown(KeyCode As Integer, _
Shift As Integer)
ShiftKey = Shift And 7
Select Case ShiftKey
Case 1 ' or vbShiftMask
Print "You pressed the SHIFT key."
Case 2 ' or vbCtrlMask
Print "You pressed the CTRL key."
Case 4 ' or vbAltMask
Print "You pressed the ALT key."
Case 3
Print "You pressed both SHIFT and CTRL."
Case 5
Print "You pressed both SHIFT and ALT."
Case 6
Print "You pressed both CTRL and ALT."
Case 7
Print "You pressed SHIFT, CTRL, and ALT."
End Select
End Sub
As long as the Textbox control has the focus, each key or combination of keys prints a corresponding message to the form when pressed.
For More Information See "Detecting SHIFT, CTRL, and ALT States" earlier in this chapter.