KeyPress Event

Applies To

CheckBox control, ComboBox control, CommandButton control, Frame control, ListBox control, MultiPage control, OptionButton control, ScrollBar control, SpinButton control, TabStrip control, TextBox control, ToggleButton control, UserForm object.

Description

Occurs when the user presses an ANSI key.

Syntax

Private Sub object_KeyPress( ByVal KeyANSI As MSForms.ReturnInteger)

The KeyPress event syntax has these parts:

Part

Description

object

Required. A valid object.

KeyANSI

Required. An integer value that represents a standard numeric ANSI key code.


Remarks

The KeyPress event occurs when the user presses a key that produces a typeable character (an ANSI key) on a running form while the form or a control on it has the focus. The event can occur either before or after the key is released. This event also occurs if you send an ANSI keystroke to a form or control using either the SendKeys action in a macro or the SendKeys statement in Visual Basic.

A KeyPress event can occur when any of the following keys are pressed:

  • Any printable keyboard character.
  • CTRL combined with a character from the standard alphabet.
  • CTRL combined with any special character.
  • BACKSPACE.
  • ESC.
A KeyPress event does not occur under the following conditions:

  • Pressing TAB.
  • Pressing ENTER.
  • Pressing an arrow key.
  • When a keystroke causes the focus to move from one control to another.
Note BACKSPACE is part of the ANSI Character Set, but DELETE is not. Deleting a character in a control using BACKSPACE causes a KeyPress event; deleting a character using DELETE doesn't.

When a user holds down a key that produces an ANSI keycode, the KeyDown and KeyPress events alternate repeatedly. When the user releases the key, the KeyUp event occurs. The form or control with the focus receives all keystrokes. A form can have the focus only if it has no controls, or if all its visible controls are disabled.

The default action for the KeyPress event is to process the event code that corresponds to the key that was pressed. KeyANSI indicates the ANSI character that corresponds to the pressed key or key combination. The KeyPress event interprets the uppercase and lowercase of each character as separate key codes and, therefore, as two separate characters.

To respond to the physical state of the keyboard, or to handle keystrokes not recognized by the KeyPress event, such as function keys, navigation keys, and any combinations of these with keyboard modifiers (ALT, SHIFT, or CTRL), use the KeyDown and KeyUp event procedures.

The sequence of keyboard-related events is:

  1. KeyDown
  2. KeyPress
  3. KeyUp
See Also

KeyDown, KeyUp events.

Example

The following example uses the KeyPress event to copy keystrokes from one TextBox to a second TextBox. The user types into the appropriately marked TextBox.

To use this example, copy this sample code to the Declarations portion of a form. Make sure that the form contains:

  • Two TextBox controls named TextBox1 and TextBox2.
    Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
        TextBox2.Text = TextBox2.Text & Chr(KeyAscii)
        
        'To handle keyboard combinations (using SHIFT, CTRL, ALT, and another 
        'key), or TAB or ENTER, use the KeyDown or KeyUp event.
    End Sub
    
    Private Sub UserForm_Initialize()
        Move 0, 0, 570, 380
        
        TextBox1.Move 30, 40, 220, 160
        TextBox1.MultiLine = True
        TextBox1.WordWrap = True
        TextBox1.Text = "Type text here."
        TextBox1.EnterKeyBehavior = True
        
        TextBox2.Move 298, 40, 220, 160
        TextBox2.MultiLine = True
        TextBox2.WordWrap = True
        TextBox2.Text = "Typed text copied here."
        TextBox2.Locked = True
    End Sub