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

Advertisements