#649 – KeyStates Property Combines IsDown and IsToggled

In a keypress handler, you can check several states for the key that triggered the event.  The IsDown and IsToggled properties indicate whether the key in question is currently down and whether it’s in the toggled state, respectively.

You can also get information on the current state of the key using the KeyStates property.  The property is an enumerated value containing a bitwise combination of the Down and Toggled values.

        private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            Console.WriteLine(string.Format("--- PreviewKeyDown for key {0}", e.Key));
            Console.WriteLine(string.Format("  IsDown={0}, IsToggled={1}", e.IsDown, e.IsToggled));
            Console.WriteLine(string.Format("  KeyStates={0}", e.KeyStates));
        }

Advertisements

#648 – Check the Toggled State of Any Key

You can use the KeyboardDevice.IsKeyToggled method to check the toggled state of any of the three toggle keys–Caps Lock, Scroll Lock, or Num Lock.

For example, we can do this in a keypress event handler for a TextBox.

        private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            Console.WriteLine(string.Format("Toggle info: Caps Lock:{0}, Scroll Lock: {1}, Num Lock: {2}",
                e.KeyboardDevice.IsKeyToggled(Key.CapsLock),
                e.KeyboardDevice.IsKeyToggled(Key.Scroll),
                e.KeyboardDevice.IsKeyToggled(Key.NumLock)));
        }

Now, as we type, we’re told of the current state of these three toggle keys.

#647 – You Can Treat Any Key As A Toggle Key

You typically use the IsToggled property in a keypress handler to check the state of a toggle key like Caps Lock.  But WPF actually keeps track of a “toggled” state for every key that you press.

For example, if you press the letter ‘a’ a number of times in a TextBox and look at the value of the IsToggled property, you’ll see that the property switches between true/false values.  WPF is keeping track of the “toggled” state for the ‘a’ key, even though it’s not normally used as a toggle key.

        private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            Trace.WriteLine(string.Format("----- PreviewKeyDown for key {0}, toggled = {1}", e.Key, e.IsToggled));
        }

#646 – Detecting a Key’s Toggle State in a Keypress Handler

There are several keys on the keyboard that typically act as toggles–when you first press the key, it is considered toggled, or on–and when you press it again, it is considered untoggled, or off.

The keys that are typically used as toggle keys are: Caps Lock, Scroll Lock and Num Lock.  (Note–the normal Shift key is not typically used as a toggle key).

When a key is pressed, you can determine whether it is entering the toggled state or the untoggled state by checking the KeyEventArgs.IsToggled property in one of the keypress event handlers.

        private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            Trace.WriteLine(string.Format("----- PreviewKeyDown for key {0}, toggled = {1}", e.Key, e.IsToggled));
        }

For example, if I press the Caps Lock four times, I’ll see the following output:

The Caps Lock key is toggling on and off.