閱讀英文

共用方式為


KeyEventArgs 類別

定義

提供 KeyDownKeyUp 事件的資料。

C#
[System.Runtime.InteropServices.ComVisible(true)]
public class KeyEventArgs : EventArgs
C#
public class KeyEventArgs : EventArgs
繼承
KeyEventArgs
屬性

範例

下列程式碼範例示範如何偵測按鍵按下的狀態。

範例 1

下列程式碼範例示範如何使用 KeyDown 事件搭配 Help 類別來顯示應用程式的快顯樣式說明。 此範例會 KeyEventArgs 使用傳遞至事件處理常式方法的屬性,篩選使用修飾詞按鍵按下 F1 鍵的所有變化。 當使用者按下包含任何鍵盤修飾詞的任何 F1 變化時, Help 類別會顯示類似 ToolTip 控制項附近的快顯視窗。 如果使用者按下 ALT+F2,則會顯示其他 [說明] 快顯視窗並顯示其他資訊。

C#
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    // Determine whether the key entered is the F1 key. If it is, display Help.
    if(e.KeyCode == Keys.F1 && (e.Alt || e.Control || e.Shift))
    {
        // Display a pop-up Help topic to assist the user.
        Help.ShowPopup(textBox1, "Enter your name.", new Point(textBox1.Bottom, textBox1.Right));
    }
    else if(e.KeyCode == Keys.F2 && e.Modifiers == Keys.Alt)
    {
        // Display a pop-up Help topic to provide additional assistance to the user.
        Help.ShowPopup(textBox1, "Enter your first name followed by your last name. Middle name is optional.",
            new Point(textBox1.Top, this.textBox1.Left));
    }
}

範例 2

下列範例會判斷使用者是否按下 ALT+E,以及滑鼠指標是否位於 上方 TreeNode ,允許使用者編輯該 TreeNode

C#
private void treeView1_KeyDown(object sender, KeyEventArgs e)
{
   /* If the 'Alt' and 'E' keys are pressed,
      * allow the user to edit the TreeNode label. */
   if(e.Alt && e.KeyCode == Keys.E)
         
   {
      treeView1.LabelEdit = true;
      // If there is a TreeNode under the mouse cursor, begin editing. 
      TreeNode editNode = treeView1.GetNodeAt(
         treeView1.PointToClient(System.Windows.Forms.Control.MousePosition));
      if(editNode != null)
      { 
         editNode.BeginEdit();
      }
   }
}

private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
   // Disable the ability to edit the TreeNode labels.
   treeView1.LabelEdit = false;
}

範例 3

下列範例會判斷使用者是否按下非數值索引鍵,如果是,則使用 Handled 屬性取消 KeyPress 事件。

C#
// Boolean flag used to determine when a character other than a number is entered.
private bool nonNumberEntered = false;

// Handle the KeyDown event to determine the type of character entered into the control.
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    // Initialize the flag to false.
    nonNumberEntered = false;

    // Determine whether the keystroke is a number from the top of the keyboard.
    if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
    {
        // Determine whether the keystroke is a number from the keypad.
        if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
        {
            // Determine whether the keystroke is a backspace.
            if(e.KeyCode != Keys.Back)
            {
                // A non-numerical keystroke was pressed.
                // Set the flag to true and evaluate in KeyPress event.
                nonNumberEntered = true;
            }
        }
    }
    //If shift key was pressed, it's not a number.
    if (Control.ModifierKeys == Keys.Shift) {
        nonNumberEntered = true;
    }
}

// This event occurs after the KeyDown event and can be used to prevent
// characters from entering the control.
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
    // Check for the flag being set in the KeyDown event.
    if (nonNumberEntered)
    {
        // Stop the character from being entered into the control since it is non-numerical.
        e.Handled = true;
    }
}

備註

KeyEventArgs 指定使用者按下的按鍵,以及是否同步選取 CTRL、ALT 和 SHIFT) 的任何修飾 (詞按鍵,都會與每個 KeyDownKeyUp 事件一起傳遞。

當使用者按下任何按鍵時,就會發生此 KeyDown 事件。 當使用者釋放金鑰時,就會發生此 KeyUp 事件。 每次重複金鑰時,如果金鑰保持關閉,但使用者放開金鑰時只會產生一個 KeyUp 事件,就會發生重複 KeyDown 的事件。

按下按鍵時也會發生此 KeyPress 事件。 KeyPressEventArgs會隨著每個 KeyPress 事件一起傳遞,並指定由每個按鍵按下所組成之字元。

如需事件模型的相關資訊,請參閱 處理和引發事件

建構函式

KeyEventArgs(Keys)

初始化 KeyEventArgs 類別的新執行個體。

屬性

Alt

取得值,指出是否按下 ALT 鍵。

Control

取得值,指出是否按下 CTRL 鍵。

Handled

取得或設定值,指出是否處理事件。

KeyCode

取得 KeyDownKeyUp 事件的鍵盤程式碼。

KeyData

取得 KeyDownKeyUp 事件的按鍵資料。

KeyValue

取得 KeyDownKeyUp 事件的鍵盤值。

Modifiers

取得 KeyDownKeyUp 事件的輔助鍵旗標。 這些旗標是表示按下 CTRL、SHIFT 和 ALT 哪些按鍵組合。

Shift

取得值,指出是否按下 SHIFT 鍵。

SuppressKeyPress

取得或設定值,指出按鍵事件是否應該傳遞至基礎控制項。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

產品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

另請參閱