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 전달 됩니다. A KeyEventArgs 각 함께 전달 되 면 KeyDownKeyUp 이벤트입니다. KeyEventArgs 보조키 (CTRL, shift 키 또는 ALT) 다른 키와 함께 눌렀는지 여부를 지정 합니다. (이 한정자 정보를 통해 가져올 수도 있습니다는 ModifierKeys 의 속성을 Control 클래스.)

설정 Handledtrue 취소 하는 KeyPress 이벤트입니다. 이렇게 하면 컨트롤이 키 누름을 처리 합니다.

참고

일부 컨트롤에 특정 키 입력을 처리할 KeyDown합니다. 예를 들어 RichTextBox 하기 전에 Enter 키를 처리 KeyPress 라고 합니다. 이러한 경우에는 취소할 수 없습니다는 KeyPress 이벤트에서 키 입력을 취소 해야 KeyDown 대신 합니다.

이벤트 모델에 대 한 자세한 내용은 이벤트 처리 및 발생합니다.

생성자

KeyPressEventArgs(Char)

KeyPressEventArgs 클래스의 새 인스턴스를 초기화합니다.

속성

Handled

KeyPress 이벤트가 처리되었는지 여부를 나타내는 값을 가져오거나 설정합니다.

KeyChar

누른 키에 해당하는 문자를 가져오거나 설정합니다.

메서드

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

적용 대상

추가 정보