Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #6: Monitoring for Keyboard and Mouse Events.

Silverlight 2 provides full support for keyboard and mouse event handling. Not only can you monitor events for the entire application but you can also monitor them at the individual control level.

The following is an example of how to monitor all the keyboard and mouse events available at the application level:

Page.xaml.cs:

namespace SilverlightApplication
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
 
            this.KeyDown                += new KeyEventHandler(Page_KeyDown);
            this.KeyUp                  += new KeyEventHandler(Page_KeyUp);
            this.MouseEnter             += new MouseEventHandler(Page_MouseEnter);
            this.MouseLeave             += new MouseEventHandler(Page_MouseLeave);
            this.MouseLeftButtonDown    += new MouseButtonEventHandler(Page_MouseLeftButtonDown);
            this.MouseLeftButtonUp      += new MouseButtonEventHandler(Page_MouseLeftButtonUp);
            this.MouseMove              += new MouseEventHandler(Page_MouseMove);
        }
 
        void Page_KeyDown(object sender, KeyEventArgs e)
        {
        }
 
        void Page_KeyUp(object sender, KeyEventArgs e)
        {
        }
 
        void Page_MouseEnter(object sender, MouseEventArgs e)
        {
        }
 
        void Page_MouseLeave(object sender, MouseEventArgs e)
        {
        }
 
        void Page_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
        }
 
        void Page_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
        }
 
        void Page_MouseMove(object sender, MouseEventArgs e)
        {
        }
    }
}

To monitor at the control level you can add an event handler directly to the control in the XAML. In the example below, we have added a mouse left down and mouse left up button event handler to a <Textblock> control.

Page.xaml:

<UserControl x:Class="SilverlightApplication6.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <TextBlock x:Name="FPS" MouseLeftButtonDown="FPS_MouseLeftButtonDown" 
                   MouseLeftButtonUp="FPS_MouseLeftButtonUp">Hello there</TextBlock>
    </Grid>
</UserControl>

Page.xaml.cs:

namespace SilverlightApplication
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
        }
 
        private void FPS_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            FPS.Text = "Mouse button down";
        }
 
        private void FPS_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            FPS.Text = "Mouse button up";
        }
    }
}

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

GCDev said:

Why there is not "while the keyDown" events ?

Gab

# April 1, 2008 7:11 PM

mike.snow said:

You will know the key is down because the KeyUp event hasn't been fired yet. That is, once you receive the KeyUp event then you know the key is no longer down.

# April 1, 2008 7:17 PM

Dew Drop - April 2, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - April 2, 2008 | Alvin Ashcraft's Morning Dew

# April 2, 2008 9:45 AM

cjefferies said:

It would nice to see an example of handling a double click event in silverlight 2. I believe the only way to do it is to handle the MouseLeftButtonDown event and perform a calculation to determine the interval between two clicks. If the interval is small enough, handle it as a double click.

# April 2, 2008 12:05 PM

14 Silverlight Tips | DavideZordan.net said:

Pingback from  14 Silverlight Tips | DavideZordan.net

# July 2, 2008 4:33 AM

Silverlight - Tip of the Day by Mike Snow at Blog von J??rgen Ebner said:

Pingback from  Silverlight - Tip of the Day by Mike Snow at Blog von J??rgen Ebner

# July 28, 2008 7:06 AM