See Also Example
Form, check box, combo box , command button , directory list box , drive list box , file list box , grid , horizontal scroll bar , list box , OLE control , option button , picture box , text box , vertical scroll bar .
Occurs when the user presses and releases an ANSI key.
Sub Form_KeyPress (KeyAscii As Integer)
Sub control_KeyPress ([Index As Integer,]KeyAscii As Integer)
The KeyPress event uses these arguments:
Index Uniquely identifies a control if it is in a control array8G7Y0UU.
KeyAscii Returns a standard numeric ANSI keycode. KeyAscii is passed by reference; changing it sends a different character to the object. Changing KeyAscii to 0 cancels the keystroke so that the object receives no character.
The object with the focus A form can receive the event only if it has no visible and enabled controls, or if the KeyPreview property is True. A KeyPress event can involve any printable keyboard character, the Ctrl key combined with a character from the standard alphabet or one of a few special characters, and the Enter or Backspace key. A KeyPress event procedure is useful for intercepting keystrokes entered in a text box or combo box. It allows you to immediately test keystrokes for validity or to format characters as they are typed. Changing the value of the argument KeyAscii changes the character displayed. receives the event.
You can convert the argument KeyAscii into a character by using the expression:
You can then perform string operations and translate the character back to an ANSI number that the control can understand by using the expression:
KeyAscii = Asc(char)
Use KeyDown and KeyUp Unlike the KeyDown and KeyUp events, KeyPress does not indicate the physical state of the keyboard; instead, it passes a character. event procedures to handle any keystroke not recognized by KeyPress, such as function keys , editing keys , navigation keys , and any combinations of these with keyboard modifiers .
KeyPress interprets the uppercase and lowercase of each character as separate keycodes and, therefore, as two separate characters. KeyDown and KeyUp interpret the uppercase and lowercase of each character by means of two arguments: KeyCode, which indicates the physical key (thus returning "A" and "a" as the same key); and Shift, which indicates the state of Shift+key and therefore returns either "A" or "a."
If the KeyPreview Use the KeyPreview property to create global keyboard-handling routines. property is True, a form receives the event before controls on the form receive the event.
Note The ANSI number for the keyboard combination of Ctrl+@ is 0. Because Visual Basic recognizes a KeyAscii value of 0 as a null string, you should avoid using Ctrl+@ in your applications.
Chr, Chr$ Function
Chapter 3, "Creating and Using Controls"
KeyPress Event Example
The example converts text entered into a text box to uppercase. To try this example, paste the code into the Declarations section of a form that contains a text box. Then press F5 and type something into the text box.
Sub Text1_KeyPress (KeyAscii As Integer)
Char = Chr(KeyAscii)
KeyAscii = Asc(UCase(Char))