Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #23 – How to Capture the Mouse Wheel Event

Silverlight currently does not support mouse wheel events. However, you can attach an event to capture the mouse wheel movement through the HtmlPage object. This tutorial will show you how to do it for IE, Opera, Mozilla and Safari browsers.

To start, we declare three events to capture the mouse event in order to cover all the possibilities for all browsers. For example, the DOMMouseScroll event is used by Mozilla.

public Page()
{
    InitializeComponent();
 
    HtmlPage.Window.AttachEvent("DOMMouseScroll", OnMouseWheel);
    HtmlPage.Window.AttachEvent("onmousewheel", OnMouseWheel);
    HtmlPage.Document.AttachEvent("onmousewheel", OnMouseWheel);
}

In our event we can get the delta change in the wheel. The way we do this in Mozilla and Safari browsers is different than how we do it in IE or Opera.

  1. Mozilla/Safari: Check the property called “detail”
  2. IE/Opera: Check the property called “wheeldelta”

Here is the complete code:

private void OnMouseWheel(object sender, HtmlEventArgs args)
{
    double mouseDelta = 0;
    ScriptObject e = args.EventObject;
 
    // Mozilla and Safari
    if (e.GetProperty("detail") != null) 
    {
        mouseDelta = ((double)e.GetProperty("detail"));
    }
    // IE and Opera
    else if (e.GetProperty("wheelDelta") != null) 
        mouseDelta = ((double)e.GetProperty("wheelDelta"));
 
    mouseDelta = Math.Sign(mouseDelta);
}

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

Silverlight currently does not support mouse wheel events. However, you can attach an event to capture

# July 30, 2008 12:29 AM

samcov said:

Can you use the same technique to capture the right mouse click?

# July 30, 2008 3:30 AM

Dew Drop - July 30, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - July 30, 2008 | Alvin Ashcraft's Morning Dew

# July 30, 2008 8:07 AM

Community Blogs said:

Mark Monster on cross-comain-scripting issues, Page Brooks on his treadmill, Mike Snow on the Mouse Wheel

# July 30, 2008 12:17 PM

Visual Web Developer Team Blog said:

7 new Silverlight blogs have been completed. Check them out and let me know if you have any questions.

# August 14, 2008 2:51 PM

Flash vs Silverlight: 3D Image Navigation said:

Pingback from  Flash vs Silverlight: 3D Image Navigation

# September 2, 2008 4:09 PM

Page Brooks said:

Silverlight Contrib: Targets for Next Release

# September 15, 2008 10:34 PM

Silverlight Tips of the Day - Blog by Mike Snow said:

In Tip of the Day #23 I showed you how to capture the mouse wheel event. In this tip we will take it

# October 22, 2008 2:32 PM

Mouse Wheel Events at Blog von J??rgen Ebner said:

Pingback from  Mouse Wheel Events at Blog von J??rgen Ebner

# October 23, 2008 4:52 PM

mr.saif said:

nice tip it helped :)

# November 5, 2008 4:37 AM

MultiScaleImage: Flex Deep Zoom Component — RTFM / Daniel Gasienica said:

Pingback from  MultiScaleImage: Flex Deep Zoom Component — RTFM / Daniel Gasienica

# December 11, 2008 10:26 AM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:57 PM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:57 PM

o UAU nosso de cada dia said:

essa lista eu copiei desse blog bárbaro (acompanhe por RSS você também): uma lista de dicas super úteis

# January 3, 2009 6:25 AM

How to enable mouse wheel scrolling in Silverlight without extending controls « Kiener’s Blog said:

Pingback from  How to enable mouse wheel scrolling in Silverlight without extending controls « Kiener’s Blog

# May 18, 2009 10:14 AM