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

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

3 Responses to #645 – Checking for the Presence of Modifier Keys

  1. AJ says:

    Your website contains a lot of useful info — impressive! One question. Do you know whether your proposed solution (using e.KeyboardDevice.Modifiers) obtains the state of the modifiers when the KeyDown event occurred, or the current state of the modifiers? If it obtains the current state of the modifiers, that can cause a bug when the system is under load and the invocation of the KeyDown event is delayed.

    • Sean says:

      AJ, I believe that Modifiers reports the current state of the keyboard. You won’t run into problems, though, because you’d have pressed Alt or Ctrl before the key for which you define the PreviewKeyDown event. So if the user pressed Alt/Ctrl before another key, Modifiers would always correctly represent the Alt/Ctrl state by the time that you get PreviewKeyDown.

  2. Pingback: #1,076 – Two Ways to Check for Use of Modifier Keys in Keypress Handlers | 2,000 Things You Should Know About WPF

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: