The KeyPress event happens after
the KeyDown event but before the KeyUp event.
It detects the ASCII value of the character
generated by the pressed key.
The KeyPress event procedure's
single parameter is KeyAscii. KeyAscii is an
integer representing the ASCII value of the
character generated by the user's key
press.
For instance, if the user keys
an uppercase "A," then the KeyPress
event fires and the KeyAscii parameter will
have a value of 65 (since 65 is the ASCII code
for uppercase "A").
If you write code in the KeyPress
event that changes the value of KeyAscii, then
the system will see the newly assigned character
as the character that the user has just typed.
If you change the value of KeyAscii
to 0, then the system will not see a keystroke,
and you have in effect discarded the keystroke.
The preceding discussion implies the following general technique
for handling user keyboard input in the KeyPress event procedure:
-
Use the Chr function to convert KeyAscii
to a character value.
-
Manipulate or evaluate the character.
-
Use the Asc function to convert the changed
character back to its corresponding integer
value, or determine the desired ASCII value
in some other way.
-
Assign the new ASCII value to the KeyAscii
parameter.
In Listing 5.1, all characters keyed in by the user are converted
to lowercase in the following steps:
-
Convert KeyAscii to its character equivalent
with the Chr function.
-
Convert the newly derived character to
lowercase.
-
Convert the lowercase character back to
an ASCII value with the Asc function and
reassign the lowercase ASCII value back
to KeyAscii.
LISTING 5.1
CHANGING THE CASE OF A CHARACTER IN THE KEYPRESS EVENT PROCEDURE
Private Sub txtPassword_KeyPress(KeyAscii
As Integer)
Dim KeyChar As String
KeyChar = Chr$(KeyAscii) 'convert to character
KeyChar = LCase(KeyChar) 'change to lowercase
KeyAscii = Asc(KeyChar) 'reassign changed
ASCII
End Sub
Listing 5.2 uses a similar technique
to allow the user to input only digits. The
code checks to see whether the user has keyed
in a numeric character. If not, the code discards
the character by changing the value of KeyAscii
to 0.
Notice that the outer branch
condition in the listing (KeyAscii > 31)
allows lower-numbered ASCII characters to pass
through. This is necessary because some of the
control keys, such as Backspace, generate low
ASCII codes (Backspace generates ASCII 8). If
the event procedure discarded these characters,
then the user would not be able to use Backspace
or some other keys.
LISTING 5.2
DISCARDING SELECTED KEYSTROKES IN THE KEYPRESS EVENT PROCEDURE
Private Sub txtPassword_KeyPress(KeyAscii
As Integer)
Dim KeyChar As String
If KeyAscii > 31 Then 'ignore low-ASCII
characters like
åBACKSPACE
KeyChar = Chr(KeyAscii)
If Not IsNumeric(KeyChar) Then
KeyAscii = 0
End If
End If
End Sub
The KeyPress event only fires if the key that is pressed generates
an ASCII character. There are many keys on the keyboard that do not generate ASCII
characters including all of the function keys and most of the cursor movement
keys. To detect those key presses, you must use the KeyUp and KeyDown events.
Keystroke Events at Field and Form Level topics