KeyEventArgs.KeyStates 屬性

定義

取得與這個事件關聯的鍵盤按鍵狀態。

public:
 property System::Windows::Input::KeyStates KeyStates { System::Windows::Input::KeyStates get(); };
public System.Windows.Input.KeyStates KeyStates { get; }
member this.KeyStates : System.Windows.Input.KeyStates
Public ReadOnly Property KeyStates As KeyStates

屬性值

按鍵的狀態。

範例

下列範例會藉由對KeyStates索引鍵和Down列舉值的 執行位AND比較,來檢查 與 實例KeyEventArgs相關聯的索引鍵是否關閉。

// A bitwise AND operation is used in the comparison.
// e is an instance of KeyEventArgs.
// btnDown is a Button.
if ((e.KeyStates & KeyStates.Down) > 0)
{
    btnDown.Background = Brushes.Red;
}
' A bitwise AND operation is used in the comparison.
' e is an instance of KeyEventArgs.
' btnDown is a Button.
If (e.KeyStates And KeyStates.Down) > 0 Then
    btnDown.Background = Brushes.Red

備註

索引鍵可以同時處於向上和切換狀態或向下和切換狀態。 基於這個理由,判斷索引鍵是向上還是向下,不像將值檢查 KeyStates 為數值一樣簡單。 相反地,您應該將其視為旗標列舉來檢查值。 使用第一個 AND 位的比較。 或者,使用協助程序屬性 IsUpIsDownIsToggled 來判斷指定的索引鍵是否為向上、向下或切換。

適用於

另請參閱