Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #31: How to Detect Alt, Shift, Control, Windows and Apple keys with Left Mouse Down in Silverlight

When clicking on your Silverlight application how do you know if the <Alt>, <Shift>, <Ctrl>, <Windows> and/or <Apple> key is down as well?

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

The following code below in our Page.xaml.cs shows how this is done.

Run and preview this app here: http://silverlight.services.live.com/invoke/66033/Left%20Mouse%20Down/iframe.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
 
namespace ShiftMouseClick
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
 
            this.MouseLeftButtonDown += new MouseButtonEventHandler(Page_MouseLeftButtonDown);
        }
 
        void Page_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Data.Text = String.Empty;
 
            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;
 
            if (true == shiftKey)
                Data.Text += "<shift>";
            if (true == altKey)
                Data.Text += "<alt>";
            if (true == appleKey)
                Data.Text += "<apple>";
            if (true == controlKey)
                Data.Text += "<ctrl>";
            if (true == windowsKey)
                Data.Text += "<windows>";
 
            Data.Text += " Left Clicked";
 
        }
    }
}

And our Page.xaml:

<UserControl x:Class="ShiftMouseClick.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="800" Height="600">
    <Grid x:Name="LayoutRoot" Background="White">
        <TextBlock x:Name="Data">Click here (also use alt,ctrl,windows,apple,shift keys)</TextBlock>
    </Grid>
</UserControl>

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

When clicking on your Silverlight application how do you know if the &lt;Alt&gt;, &lt;Shift&gt;, &lt;Ctrl&gt;

# August 25, 2008 3:20 PM

Silverlight news for August 26, 2008 said:

Pingback from  Silverlight news for August 26, 2008

# August 26, 2008 3:08 AM

Mirrored Blogs said:

Post: Approved at: Aug-26-2008 Tip: Detect ALt, Shift, Control, etc keys When clicking on your Silverlight

# August 26, 2008 4:12 AM

Dew Drop - August 26, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - August 26, 2008 | Alvin Ashcraft's Morning Dew

# August 26, 2008 8:32 AM

Visual Web Developer Team Blog said:

Silverlight Tip of the Day #32 &#160; Title : How to Declare a Custom User Control from a XAML Page.

# August 26, 2008 1:13 PM

News said:

Silverlight Tip of the Day #32 Title : How to Declare a Custom User Control from a XAML Page. Demo :

# August 26, 2008 2:57 PM

Community Blogs said:

Scott Barnes on Submitting SL Bugs, Shawn Wildermuth on SL Firestarter in NYC, Jason Cooke Templatinging

# August 26, 2008 8:35 PM

Odegaard said:

I love it when people compare booleans to true/false values.

Why not just do: if (shiftKey) ...

Simpler and more readable.

# September 3, 2008 1:56 PM

mike.snow said:

Odegaard  - You can do it anyway you like. Personally, I find it easier to read when scanning code if it compares to true/false.

# September 4, 2008 1:07 AM

SilverTrader: AG_E_NETWORK_ERROR « Tales from a Trading Desk said:

Pingback from  SilverTrader: AG_E_NETWORK_ERROR   &laquo; Tales from a Trading Desk

# November 17, 2008 2:54 PM