System.Windows.Forms.KeyPressEventArgs Class

Provides data for the Control.KeyPress event.

See Also: KeyPressEventArgs Members

Syntax

[System.Runtime.InteropServices.ComVisible(true)]
public class KeyPressEventArgs : EventArgs

Remarks

A System.Windows.Forms.KeyPressEventArgs specifies the character that is composed when the user presses a key. For example, when the user presses SHIFT + K, the KeyPressEventArgs.KeyChar property returns an uppercase K.

A Control.KeyPress event occurs when the user presses a key. Two events that are closely related to the Control.KeyPress event are Control.KeyUp and Control.KeyDown. The Control.KeyDown event precedes each Control.KeyPress event when the user presses a key, and a Control.KeyUp event occurs when the user releases a key. When the user holds down a key, duplicate Control.KeyDown and Control.KeyPress events occur each time the character repeats. One Control.KeyUp event is generated upon release.

With each Control.KeyPress event, a System.Windows.Forms.KeyPressEventArgs is passed. A System.Windows.Forms.KeyEventArgs is passed with each Control.KeyDown and Control.KeyUp event. A System.Windows.Forms.KeyEventArgs specifies whether any modifier keys (CTRL, SHIFT, or ALT) were pressed along with another key. (This modifier information can also be obtained through the Control.ModifierKeys property of the System.Windows.Forms.Control class.)

Set KeyPressEventArgs.Handled to true to cancel the KeyPress event. This keeps the control from processing the key press.

Note:

Some controls will process certain key strokes on Control.KeyDown. For example, System.Windows.Forms.RichTextBox processes the Enter key before Control.KeyPress is called. In such cases, you cannot cancel the Control.KeyPress event, and must cancel the key stroke from Control.KeyDown instead.

For information about the event model, see [<topic://cpconEventsDelegates>].

Requirements

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0