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 建立 控制項。 方法 keypressedKeyChar 使用 屬性來檢查是否按下 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 鍵。

  • INSERT 和 DELETE。

  • 家。

  • 結束。

  • PAGE UP 和 PAGE DOWN。

  • F1-F2。

  • ALT。

  • 方向鍵

注意

如需如何偵測上述任何非字元索引鍵的資訊,請參閱 類別 KeyEventArgs

適用於

另請參閱