1 0 Tag Archives: keyboard
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

Leave a Comment