Export (0) Print
Expand All

KeyPressEventArgs Class

Provides data for the KeyPress event.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

[ComVisibleAttribute(true)]
public class KeyPressEventArgs : EventArgs

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

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

With each KeyPress event, a KeyPressEventArgs is passed. A KeyEventArgs is passed with each KeyDown and KeyUp event. A 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 ModifierKeys property of the Control class.)

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

NoteNote:

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

For information about the event model, see Events and Delegates.

The following example illustrates using the KeyPressEventArgs to count keys as they are pressed and to display the results after each key press. Handled is then set to true to keep the operating system from further processing the key. The example assumes a form with a TextBox placed on it.

public class myKeyPressClass 
 {
     static long keyPressCount = 0 ;
     static long backspacePressed =  0;
     static long returnPressed = 0 ;
     static long escPressed = 0 ;
     private TextBox textBox1 = new TextBox();
     private void myKeyCounter(object sender, KeyPressEventArgs ex)
     {
     switch(ex.KeyChar)
     {
             // Counts the backspaces. 
         case '\b':
         backspacePressed = backspacePressed + 1;
         break ;
             // Counts the ENTER keys. 
         case '\r':
         returnPressed = returnPressed + 1 ;
         break ;
             // Counts the ESC keys.   
         case (char)27:
         escPressed = escPressed + 1 ;
         break ;
             // Counts all other keys. 
         default:
         keyPressCount = keyPressCount + 1 ;
         break;
     }

     textBox1.Text = 
         backspacePressed + " backspaces pressed\r\n" + 
         escPressed + " escapes pressed\r\n" +
         returnPressed + " returns pressed\r\n" +
         keyPressCount + " other keys pressed\r\n" ;
     ex.Handled = true ;
     }
 }

You must create a new instance of this class. You must also set the event handler. You can do this in the constructor for your class.

myKeyPressClass myKeyPressHandler = new myKeyPressClass();
public Form1()
{
     InitializeComponent();

     textBox1.KeyPress += new KeyPressEventHandler(myKeyPressHandler.myKeyCounter);
}

When the specified event is raised in the control, the attached method is called and the application can execute code in response to the event.

System.Object
  System.EventArgs
    System.Windows.Forms.KeyPressEventArgs

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft