28
Aug
07

Detecting the mouse scroll wheel in a Flex application

The following example demonstrates a very simple Flex application which listens for the mouseWheel event (represented here by the MouseEvent.MOUSE_WHEEL constant) and updates a Label control on the display list.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/08/28/detecting-the-mouse-scroll-wheel-in-a-flex-application/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init()">

    <mx:Script>
        <![CDATA[
            private function init():void {
                systemManager.addEventListener(MouseEvent.MOUSE_WHEEL, doMouseWheel);
            }

            private function doMouseWheel(evt:MouseEvent):void {
                num += evt.delta;
            }
        ]]>
    </mx:Script>

    <mx:Number id="num">0</mx:Number>

    <mx:NumberFormatter id="numberFormatter" />

    <mx:ApplicationControlBar dock="true">
        <mx:Label text="Click the stage and scroll mouse wheel to begin." />
    </mx:ApplicationControlBar>

    <mx:Label text="{numberFormatter.format(num)}"
            fontSize="96" />

</mx:Application>

View source is enabled in the following example.

Oops, I had to remove the SWF since it was competing with IE’s scroll (I’m using a weird <iframe /> solution for inline samples). To see the sample, click here.

And no, the number has absolutely no significance. It is just a quick way to see if the mouse wheel was responding.


6 Responses to “Detecting the mouse scroll wheel in a Flex application”


  1. 1 Tony Fendall Aug 28th, 2007 at 6:08 pm

    For me, this works in FireFox, but not in IE7 or Opera (all on Windows XP)

  2. 2 Blanka Sep 16th, 2007 at 5:58 am

    I tried this code in Firefox Safari on OS X and it didn’t work. The number in the middle didn’t change despite clicks scrolls. I added an “Alert.show(”Scrolled”);” line after num = evt;, but that Alert didn’t pop up either.

    Any tips to get this to work?

    [Separately, I tried just clicking the sample application on this page — that didn’t work for me either.]

  3. 3 Blanka Sep 16th, 2007 at 4:02 pm

    Turns out this is a known issue in OS X: http://hasseg.org/blog/?p=3

  4. 4 Dusan Apr 9th, 2008 at 7:52 am

    hello, is there any way to detect middle-mouse click?

  5. 5 gcardinal Apr 16th, 2008 at 12:43 am

    Hello! Great work! Would be great to have a middle-mouse click as well! thanks

  6. 6 walv Nov 29th, 2008 at 12:20 pm

    4 Blanka, Tony Fendall:
    Guys, try to click before scrolling. It is related with mouse focus.
    It works for me.
    Regards

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".




Badge Farm

  • Firefox 2
  • Powered by Redoable 1.2
  • Feeds burnt by Feedburner
  • Feed