KeyStates is a bit field enumeration; therefore, it is possible for a key to be in multiple states. For example, a key could be in the pressed state as well as in the toggled state. Use bit comparison operations to determine the exact state or states the key is in.
The following example shows how to use the GetKeyStates method to determine if the Return key is in the Down state. A bit AND operation is used to compare the KeyStates returned from GetKeyStates and the Down state. If the key is down, the background of a Button is changed.
// Uses the Keyboard.GetKeyStates to determine if a key is down. // A bitwise AND operation is used in the comparison. // e is an instance of KeyEventArgs. if ((Keyboard.GetKeyStates(Key.Return) & KeyStates.Down) > 0)
btnNone.Background = Brushes.Red;