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