Export (0) Print
Expand All
2 out of 5 rated this helpful - Rate this topic

KeyPressEventArgs Class

Provides data for the KeyPress event.

For a list of all members of this type, see KeyPressEventArgs Members.

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

[Visual Basic]
<ComVisible(True)>
Public Class KeyPressEventArgs
   Inherits EventArgs
[C#]
[ComVisible(true)]
public class KeyPressEventArgs : EventArgs
[C++]
[ComVisible(true)]
public __gc class KeyPressEventArgs : public EventArgs
[JScript]
public
   ComVisible(true)
class KeyPressEventArgs extends EventArgs

Thread Safety

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

Remarks

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.

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

.NET Compact Framework Platform Note:  The KeyPress event for combinations using the Ctrl-Shift keys is not provided. Instead, you can trap the KeyDown and KeyUp events.

Example

[Visual Basic, C#, C++] 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.

[Visual Basic] 
Public Class myKeyPressClass
    Private Shared keyPressCount As Long = 0
    Private Shared backspacePressed As Long = 0
    Private Shared returnPressed As Long = 0
    Private Shared escPressed As Long = 0
    Private textBox1 As TextBox
    
    Private Sub myKeyCounter(sender As Object, ex As KeyPressEventArgs)
        Select Case ex.KeyChar
            ' Counts the backspaces.
            Case ControlChars.Back
                backspacePressed = backspacePressed + 1
            ' Counts the ENTER keys.
            Case ControlChars.Lf
                returnPressed = returnPressed + 1
            ' Counts the ESC keys.  
            Case Convert.ToChar(27)
                escPressed = escPressed + 1
            ' Counts all other keys.
            Case Else
                keyPressCount = keyPressCount + 1
        End Select
        
        textBox1.Text = backspacePressed & " backspaces pressed" & _
            ControlChars.Lf & ControlChars.Cr & escPressed & _
            " escapes pressed" & ControlChars.CrLf & returnPressed & _
            " returns pressed" & ControlChars.CrLf & keyPressCount & _
            " other keys pressed" & ControlChars.CrLf
        ex.Handled = True
    End Sub 'myKeyCounter
End Class 'myKeyPressClass

[C#] 
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 ;
     }
 }

[C++] 
public __gc class myKeyPressClass 
 {
     static long keyPressCount = 0 ;
     static long backspacePressed =  0;
     static long returnPressed = 0 ;
     static long escPressed = 0 ;
private:
     TextBox* textBox1;

     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 = 
         String::Concat( __box(backspacePressed), S" backspaces pressed\r\n", __box(escPressed), S" escapes pressed\r\n",
             returnPressed, S" returns pressed\r\n", keyPressCount, S" other keys pressed\r\n" ) ;
     ex->Handled = true ;
     }
 };

[Visual Basic, C#, C++] 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.

[Visual Basic] 
Private myKeyPressHandler As New myKeyPressClass()

Public Sub New()
    InitializeComponent()
    
    AddHandler textBox1.KeyPress, AddressOf myKeyPressHandler.myKeyCounter
End Sub 'New


[C#] 
myKeyPressClass myKeyPressHandler = new myKeyPressClass();
public Form1()
{
     InitializeComponent();
 
     textBox1.KeyPress += new KeyPressEventHandler(myKeyPressHandler.myKeyCounter);
}

[C++] 
myKeyPressClass* myKeyPressHandler;
public:
Form1()
{
     myKeyPressHandler = new myKeyPressClass();
     InitializeComponent();
 
     textBox1->KeyPress += new KeyPressEventHandler(myKeyPressHandler, &myKeyPressClass::myKeyCounter);
}

[Visual Basic, C#, C++] 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.

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Windows.Forms

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

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

See Also

KeyPressEventArgs Members | System.Windows.Forms Namespace | OnKeyPress | KeyPress | KeyEventArgs | OnKeyDown | KeyDown | OnKeyUp | KeyUp | ModifierKeys

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.