KeyPressEventArgs.KeyChar 속성

정의

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

public:
 property char KeyChar { char get(); };
public:
 property char KeyChar { char get(); void set(char value); };
public char KeyChar { get; }
public char KeyChar { get; set; }
member this.KeyChar : char
member this.KeyChar : char with get, set
Public ReadOnly Property KeyChar As Char
Public Property KeyChar As Char

속성 값

Char

구성된 ASCII 문자입니다. 예를 들어, 사용자가 Shift+K를 누르면 이 속성은 대문자 K를 반환합니다.

예제

다음 예제에서는 TextBox 제어 합니다. 합니다 keypressed 메서드는 KeyChar ENTER 키를 눌렀는지 여부를 확인할 속성입니다. ENTER 키를 누르는 경우는 Handled 속성 true, 처리 되는 이벤트를 나타냅니다.

#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Windows::Forms;

public ref class Form1: public Form
{
public:
   Form1()
   {
      // Create a TextBox control.
      TextBox^ tb = gcnew TextBox;
      this->Controls->Add( tb );
      tb->KeyPress += gcnew KeyPressEventHandler( this, &Form1::keypressed );
   }

private:
   void keypressed( Object^ /*o*/, KeyPressEventArgs^ e )
   {
      // The keypressed method uses the KeyChar property to check 
      // whether the ENTER key is pressed. 
      // If the ENTER key is pressed, the Handled property is set to true, 
      // to indicate the event is handled.
      if ( e->KeyChar == (char)13 )
            e->Handled = true;
   }
};

int main()
{
   Application::Run( gcnew Form1 );
}

using System;
using System.Windows.Forms;

public class Form1: Form
{
    public Form1()
    {
        // Create a TextBox control.
        TextBox tb = new TextBox();
        this.Controls.Add(tb);
        tb.KeyPress += new KeyPressEventHandler(keypressed);
    }

    private void keypressed(Object o, KeyPressEventArgs e)
    {
        // The keypressed method uses the KeyChar property to check 
        // whether the ENTER key is pressed. 

        // If the ENTER key is pressed, the Handled property is set to true, 
        // to indicate the event is handled.
        if (e.KeyChar == (char)Keys.Return)
        {
            e.Handled = true;
        }
    }

    public static void Main()
    {
        Application.Run(new Form1());
    }
}
Imports System.Windows.Forms

Public Class Form1
    Inherits Form

    Public Sub New()
        ' Create a TextBox control.
        Dim tb As New TextBox()
        Me.Controls.Add(tb)
        AddHandler tb.KeyPress, AddressOf keypressed
    End Sub

    Private Sub keypressed(ByVal o As [Object], ByVal e As KeyPressEventArgs)
        ' The keypressed method uses the KeyChar property to check 
        ' whether the ENTER key is pressed. 

        ' If the ENTER key is pressed, the Handled property is set to true, 
        ' to indicate the event is handled.

        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            e.Handled = True
        End If
    End Sub

    Public Shared Sub Main()
        Application.Run(New Form1())
    End Sub
End Class

설명

사용 된 KeyChar 샘플 키 입력을 런타임에 및 특별 한 런타임 환경에서 키 입력을 수정 하는 속성입니다. 예를 들어 사용할 수 있습니다 KeyChar 우편 번호를 입력 하면 숫자가 아닌 키를 사용 하지 않도록 설정, 데이터 항목 필드에 대문자를 모두 사전순 키 누르기를 변경 하거나 키보드 또는 특정 키 조합에 대 한 다른 키 입력된 디바이스를 모니터링 합니다.

다음 키를 설정 또는 얻을 수 있습니다.

  • a-z, A-Z.

  • CTRL 합니다.

  • 문장 부호 표시 합니다.

  • 키를 모두 숫자 키패드의 및 키보드의 위쪽 번호입니다.

  • 입력 합니다.

다음 키를 설정 하거나 가져올 수 없습니다.

  • <Tab> 키입니다.

  • 삽입 및 삭제 합니다.

  • 홈입니다.

  • 끝입니다.

  • PAGE UP 및 PAGE DOWN입니다.

  • F1-F2입니다.

  • ALT 키입니다.

  • 화살표 키

참고

위에서 언급 한 문자가 아닌 키를 검색 하는 방법에 대 한 내용은 참조는 KeyEventArgs 클래스입니다.

적용 대상

추가 정보