VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

VB .NET - Learn Visual Basic .NET

Systems Analysis - System analysis and Design tutorial for Software Engineering

You are here: Visual Basic > Advanced VB6 tutorial > Chapter 5

VB - Visual Basic 6 KeyPress Event

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:

  1. Use the Chr function to convert KeyAscii to a character value.

  2. Manipulate or evaluate the character.

  3. 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.

  4. 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:

  1. Convert KeyAscii to its character equivalent with the Chr function.

  2. Convert the newly derived character to lowercase.

  3. Convert the lowercase character back to an ASCII value with the Asc function and reassign the lowercase ASCII value back to KeyAscii.


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.


Private Sub txtPassword_KeyPress(KeyAscii As Integer)
Dim KeyChar As String
If KeyAscii > 31 Then 'ignore low-ASCII characters like
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

See Also


<< Previous | Contents | Next >>

Home | Link to Us | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved