KeyPress Event

See Also91JN0KP              Example0A20W7>Low

Applies To

Form14TJ2LN, check box9P3BU5, combo box1YZXFF6, command buttonXJSPC0, directory list boxO9U5A0, drive list box5WJO0PW, file list box1M6S8UX, grid2VGT0PT, horizontal scroll bar1JSJOS7, list boxG11UCK, OLE control2HQDVVU, option buttonJYBO08, picture box31MYIWX, text boxYPYZDG, vertical scroll bar1JSJOS7.


Occurs when the user presses and releases an ANSI5221FB key.


Sub Form_KeyPress (KeyAscii As Integer)

Sub control_KeyPress ([Index As Integer,]KeyAscii As Integer)


The KeyPress event uses these arguments:

Argument     Description


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 focus1L3L8ZY receives the event.  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.

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 KeyUpRI06O6 event procedures to handle any keystroke not recognized by KeyPress, such as function keys58I6XR, editing keys2MS9DA6, navigation keys3ZZ1RT5, and any combinations of these with keyboard modifiers198ZVV6.  Unlike the KeyDown and KeyUp events, KeyPress does not indicate the physical state of the keyboard; instead, it passes a character.

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 KeyPreview18F9O8A property is True, a form receives the event before controls on the form receive the event.  Use the KeyPreview property to create global keyboard-handling routines.


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.


See Also


ANSI Table108ABF

Asc FunctionLANASC

Change Event8DZUNR

Chr, Chr$ FunctionLANCHR


KeyPreview Property18F9O8A

SendKeys Statement41LDB48


Programmer's Guide:

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))

End Sub