KeyPress Event
Object List Next Object
Defined By: Label
Description:
Occurs when the user presses and releases an ANSI key, allowing the program to
intercept and check each byte of a user's input as it is received.
Usage:
Sub Form_KeyPress (keyAscii As Integer)
Argument Description
KeyAscii Returns an integer that represents the ASCII code of the standard ANSI key
character that was pressed. KeyAscii is passed by reference, but this can be
changed to send a different character to the object. To cancel the keystroke, change
KeyAscii to 0.
Details:
The object that has the focus receives the event each time a user presses a
valid key. A form, however, cannot receive the event unless its KeyPreview
property is set to True and no controls are active.
A KeyPress event can recognize:
Any printable keyboard character.
The CTRL key plus a standard alphabetic character (a few special characters do
apply).
The ENTER or BACKSPACE key.
A KeyPress cannot recognize function keys, navigation keys, editing keys, or
combinations of these keys with keyboard modifiers such as SHIFT and ALT. Use
the KeyDown and KeyUp event procedures to handle these keystrokes. Keep in mind,
however, that while KeyDown and KeyUp can indicate the physical state of the
keyboard, KeyPress cannot. KeyPress simply passes a character.
A KeyPress event procedure allows you to react to and test keystrokes as they
are received. For example, you could intercept keystrokes entered in a text box
or combo box. You could test the intercepted keystrokes for validity and then
format the characters as they are typed. You can also change the character
displayed.
To change the character displayed, change the value of the argument KeyAscii. For example, if you only want uppercase letters entered in a particular text
box, you can use the KeyPress event for that control to change each character
to uppercase as it is entered. Unlike KeyDown and KeyUp, KeyPress interprets
the upper- and lowercase of each character as different keycodes and, therefore,
as two distinct characters.
For form-level keyboard event handling, set the KeyPreview property to True.
When set to True, the form receives the events before any controls on the form.
Note: The CTRL+@ combination should not be used in your application. Phoenix views
this combination as a null string with the ANSI number value of 0.
See Also:
Asc function, Chr, Chr$ functions, Change event