KeyPressEventArgs 类

定义

KeyPress 事件提供数据。

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

示例

以下示例演示如何使用 KeyPressEventArgs 按键时对键进行计数,并在每个键按下后显示结果。 Handled 然后设置为 true,使操作系统不进一步处理密钥。 该示例假定窗体上放置了 TextBox 一个窗体。

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
End 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

当在控件中引发指定的事件时,将调用附加的方法,应用程序可以执行代码以响应事件。

注解

KeyPressEventArgs 个指定当用户按下键时构成的字符。 例如,当用户按下 SHIFT + K 时,该 KeyChar 属性将返回大写 K。

KeyPress当用户按下键时发生事件。 与事件密切相关 KeyPress 的两个事件是 KeyUpKeyDown。 当用户 KeyDown 按下键时,该事件位于每个 KeyPress 事件前面,当用户 KeyUp 释放键时发生事件。 当用户按住某个键时,每次重复字符时都会发生重复 KeyDownKeyPress 事件。 发布后生成一个 KeyUp 事件。

对于每个 KeyPress 事件,都会传递一个 KeyPressEventArgs 。 随每个KeyDown事件一KeyUp起传递 AKeyEventArgs。 指定 KeyEventArgs 是否将任何修饰键 (Ctrl、SHIFT 或 Alt) 与另一个键一起按下。 (还可以通过 ModifierKeys class.) 的属性 Control 获取此修饰符信息

设置为Handledtrue取消KeyPress事件。 这样,控件就不处理键按下。

备注

某些控件将处理某些关键笔划。KeyDown 例如, RichTextBox 在调用之前 KeyPress 处理 Enter 键。 在这种情况下,无法取消 KeyPress 事件,并且必须取消 KeyDown 密钥笔划。

有关事件模型的信息,请参阅 “处理和引发事件”。

构造函数

KeyPressEventArgs(Char)

初始化 KeyPressEventArgs 类的新实例。

属性

Handled

获取或设置一个值,该值指示是否处理过 KeyPress 事件。

KeyChar

获取或设置与按下的键对应的字符。

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅