post icon

Silverlight Tip of the Day #8 – Detecting Alt, Shift, Control, Window & Apple Keys Combinations

When mouse clicking or typing a key in your Silverlight application how do you know if any combination of the <Alt>, <Shift>, <Ctrl>, <Windows> and/or <Apple> keys are down as well?

To accomplish this you simply need to check the Keyboard.Modifiers member which returns a ModifierKeys object.

The following code demonstrates how to get this object and how to check if any of these keys have been pressed:

ModifierKeys keys = Keyboard.Modifiers; 
 
bool shiftKey = (keys & ModifierKeys.Shift) != 0; 
bool altKey = (keys & ModifierKeys.Alt) != 0; 
bool appleKey = (keys & ModifierKeys.Apple) != 0; 
bool controlKey = (keys & ModifierKeys.Control) != 0; 
bool windowsKey = (keys & ModifierKeys.Windows) != 0;

Demo:

[silverlight: Tip8_ModiferKeys.xap]

Thank you,

–Mike

4 Comments

Leave a comment
  1. Jon
    30. Apr, 2010 at 5:14 am #

    Shift- and Ctrl-keys work. Alt alone doesn’t Alt Gr and ctrl+alt works. Windows shows
    Using english XP sp3 with swedish keyboard.

    Love the new blog!

  2. Martin
    01. Oct, 2010 at 2:44 am #

    Very useful! Thanks.

  3. Francois
    19. Jun, 2011 at 11:09 pm #

    Hi

    Why would this not work for us? We do not get the ALT modifier to fire at all, only when pressed with control does it work. Alt-A for example also does not fire a KeyDownEvent.

    This is true for in- and out-of-browser.

Trackbacks/Pingbacks

  1. Dew Drop – April 30, 2010 | Alvin Ashcraft's Morning Dew - 30. Apr, 2010

    [...] Silverlight Tip of the Day #8 – Detecting Alt, Shift, Control, Window & Apple Keys Combination… (Mike Snow) [...]