KeyPressEventArgs KeyPressEventArgs KeyPressEventArgs KeyPressEventArgs Class

定义

KeyPress 事件提供数据。Provides data for the KeyPress event.

public ref class KeyPressEventArgs : EventArgs
[System.Runtime.InteropServices.ComVisible(true)]
public class KeyPressEventArgs : EventArgs
type KeyPressEventArgs = class
    inherit EventArgs
Public Class KeyPressEventArgs
Inherits EventArgs
继承
KeyPressEventArgsKeyPressEventArgsKeyPressEventArgsKeyPressEventArgs
属性

示例

下面的示例演示如何使用KeyPressEventArgs计入密钥,因为它们被按下并在每次按键后显示结果。The following example illustrates using the KeyPressEventArgs to count keys as they are pressed and to display the results after each key press. Handled 然后设置为 true,以防止操作系统中进一步处理密钥。Handled is then set to true to keep the operating system from further processing the key. 该示例假定窗体具有TextBox在其上放置。The example assumes a form with a TextBox placed on it.

public ref class myKeyPressClass
{
private:
   static long keyPressCount = 0;
   static long backspacePressed = 0;
   static long returnPressed = 0;
   static long escPressed = 0;
   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( 
         backspacePressed, " backspaces pressed\r\n",
         escPressed, " escapes pressed\r\n",
         returnPressed, " returns pressed\r\n",
         keyPressCount, " other keys pressed\r\n" );
      ex->Handled = true;
   }
};
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 ;
     }
 }
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

必须创建此类的新实例。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.

public:
   myKeyPressClass^ myKeyPressHandler;

   Form1()
   {
      myKeyPressHandler = gcnew myKeyPressClass;

      InitializeComponent();

      textBox1->KeyPress += gcnew KeyPressEventHandler(
         myKeyPressHandler, &myKeyPressClass::myKeyCounter );
   }
myKeyPressClass myKeyPressHandler = new myKeyPressClass();
public Form1()
{
     InitializeComponent();
 
     textBox1.KeyPress += new KeyPressEventHandler(myKeyPressHandler.myKeyCounter);
}
Private myKeyPressHandler As New myKeyPressClass()

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

当控件中引发指定的事件时,会调用附加的方法和应用程序可执行代码以响应事件。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.

注解

一个KeyPressEventArgs指定当用户按下某个键构成的字符。A KeyPressEventArgs specifies the character that is composed when the user presses a key. 例如,当用户按下 SHIFT + K、KeyChar属性返回大写的 k。For example, when the user presses SHIFT + K, the KeyChar property returns an uppercase K.

一个KeyPress用户按下某个键时发生事件。A KeyPress event occurs when the user presses a key. 与密切相关的两个事件KeyPress事件都KeyUpKeyDownTwo events that are closely related to the KeyPress event are KeyUp and KeyDown. KeyDown事件先于每个KeyPress事件,当用户按下某个键,和一个KeyUp用户释放某个键时发生事件。The KeyDown event precedes each KeyPress event when the user presses a key, and a KeyUp event occurs when the user releases a key. 当用户按住某个键时,重复KeyDownKeyPress字符重复每次发生的事件。When the user holds down a key, duplicate KeyDown and KeyPress events occur each time the character repeats. 一个KeyUp发布后生成事件。One KeyUp event is generated upon release.

与每个KeyPress事件,KeyPressEventArgs传递。With each KeyPress event, a KeyPressEventArgs is passed. 一个KeyEventArgs与每个传递KeyDownKeyUp事件。A KeyEventArgs is passed with each KeyDown and KeyUp event. 一个KeyEventArgs指定另一个密钥以及是否按下任何修改键 (CTRL、 shift 键或 ALT)。A KeyEventArgs specifies whether any modifier keys (CTRL, SHIFT, or ALT) were pressed along with another key. (还可以通过获取此修饰符信息ModifierKeys属性的Control类。)(This modifier information can also be obtained through the ModifierKeys property of the Control class.)

设置Handledtrue取消KeyPress事件。Set Handled to true to cancel the KeyPress event. 这可以防止该控件处理按键。This keeps the control from processing the key press.

备注

某些控件将在处理某些击键KeyDownSome controls will process certain key strokes on KeyDown. 例如,RichTextBox处理 Enter 键之前KeyPress调用。For example, RichTextBox processes the Enter key before KeyPress is called. 在这种情况下,不能取消KeyPress事件,必须取消从击键和KeyDown相反。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 Handling and Raising Events.

构造函数

KeyPressEventArgs(Char) KeyPressEventArgs(Char) KeyPressEventArgs(Char) KeyPressEventArgs(Char)

初始化 KeyPressEventArgs 类的新实例。Initializes a new instance of the KeyPressEventArgs class.

属性

Handled Handled Handled Handled

获取或设置一个值,该值指示是否处理过 KeyPress 事件。Gets or sets a value indicating whether the KeyPress event was handled.

KeyChar KeyChar KeyChar KeyChar

获取或设置与按下的键对应的字符。Gets or sets the character corresponding to the key pressed.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

适用于

另请参阅