#650 – Getting Information About Keyboard Keys from Any Method

You can use the KeyEventArgs.KeyboardDevice property within a keypress handler to get information about any keyboard key.  For example:

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)));
}

You can also access the same KeyboardDevice object from any method, using the Keyboard.PrimaryDevice property (found in System.Windows.Input). This property will refer to an instance of a KeyboardDevice object.

        public void MyFunctionToGetKeyInfo()
        {
            Console.WriteLine(string.Format("Toggle info: Caps Lock:{0}, Scroll Lock: {1}, Num Lock: {2}",
                Keyboard.PrimaryDevice.IsKeyToggled(Key.CapsLock),
                Keyboard.PrimaryDevice.IsKeyToggled(Key.Scroll),
                Keyboard.PrimaryDevice.IsKeyToggled(Key.NumLock)));
        }
Advertisements

#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));
        }

#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.

#645 – Checking for the Presence of Modifier Keys

In Windows, a modifier key is a key that you press in combination with another key.  The standard modifier keys in Windows are:

  • Alt key
  • Control (Ctrl) key
  • Shift key
  • Windows key (Windows logo on face of key)

In a WPF keypress handler (PreviewKeyDown, KeyDown, PreviewKeyUp and KeyUp), you can check for the presence of one of the modifier keys using the KeyEventArgs.KeyboardDevice.Modifiers property.  This property is an enumerated type that is a bitwise combination of the possible values.  You can check for the presence of one of the modifiers by doing an AND operation against the modifier.

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

            // Dumping out all modifiers
            Trace.WriteLine(string.Format("  KeyboardDevice.Modifiers = {0}", e.KeyboardDevice.Modifiers);

            // Checking for specific modifiers
            if ((e.KeyboardDevice.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)
                Trace.WriteLine(" Alt key !");

            if ((e.KeyboardDevice.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
                Trace.WriteLine(" Ctrl key !");
        }

Pressing Ctrl+Alt+G yields:

#644 – Disabling and Hiding Controls

You can disable a control by setting its IsEnabled property to false.  Disabled controls cannot receive input, but are still displayed in the user interface, typically greyed out.

Below is an example of some typical controls, showing what they looked like when enabled vs. disabled.

In addition to disabling a control, you can also make the entire control invisible, using the Visibility property.  Controls that are not visible can be one of:

  • Hidden – Control is not visible, but it still takes up space in the user interface.  I.e. There is whitespace where the control would be if it were visible
  • Collapsed – Control is not present at all in the user interface and neighboring controls fill in the space where the control would be if it were visible