Hack 62 Right and Middle Mouse Buttons

figs/moderate.gif figs/hack62.gif

Flash's documented mouse event handlers react to the primary mouse button only. Detect additional mouse buttons with the undocumented ActionScript.

Although Windows mice have two or more buttons, Flash detects mouse click events for the primary button only (usually the left mouse button, although the reader can change this in the Windows Mouse control panel).

Class of 800

There is no documented way to access the right and middle mouse buttons. Fortunately, the undocumented ASnative( ) function can access additional internal ActionScript methods [Hack #83] using numeric indexes that roughly correlate to built-in classes.

Passing the index 800 as an argument to the ASnative( ) function seems to access input/output methods of the Key and Mouse classes. It also includes some undocumented goodies. Try the following:

this.onEnterFrame = function( ) {

  if (ASnative(800, 2)(1)) {

    trace("Detected something...");

  }

};

If you run this code and click the left mouse button, the script displays "Detected something...". So ASnative(800, 2)(1) returns the left mouse button state: 1 (true) for down, or 0 (false) for up. Even though you can detect left mouse button clicks with the documented Mouse.onMouseDown( ) event listener, the preceding code lets you detect the left mouse button's current state (for example, if you want to check whether the left mouse button is still down without setting a flag and waiting for the Mouse.onMouseUp( ) event).

Changing the last argument in the ASnative( ) call from 1 to 2 is even more interesting?it detects the right mouse button state:

this.onEnterFrame = function( ) {

  if (ASnative(800, 2)(2)) {

    trace("Right-click!");

  }

};

If the final argument is 4, the ASnative( ) call returns a value that toggles every time you click or release the middle button in a three-button mouse. Most Windows mice with a wheel can also use the wheel as the third button; depressing the wheel is equivalent to "clicking button 3."

this.onEnterFrame = function( ) {

  if (ASnative(800, 2)(4)) {

    trace("Middle mouse button has changed state");

  }

};

So now we can detect the left, right, and middle button states! If your mouse has more than three buttons, use ASnative(800, 2)(5) and ASnative(800, 2)(6) to detect the states of additional buttons. The code ASnative(800, 2)(3) doesn't seem to do anything (at least, not when you use a mouse as your input device).

ASnative( ) is undocumented and therefore unsupported. The ASnative( ) calls described here work in Flash Player 5, 6, and 7, but they aren't thoroughly tested and are not guaranteed to be supported in future versions.


These techniques detect the mouse button state using polling (checking in the onEnterFrame( ) handler) because only the primary mouse button generates onMouseDown and onMouseUp events.

Of course, polling consumes CPU cycles so you should poll only when necessary. And you wouldn't want to add the polling code directly to a movie clip because multiple clip instances would all perform redundant polling for the mouse button state (which is the same regardless of which clip performs the polling). Instead, you could set up a centralized poller that broadcasts an event to any objects that have subscribed as a listener. The rightMousePoller.fla file, available on this book's web site, demonstrates the distinction. It has four "bad" pollers and three "good" pollers. When you right-click, all seven pollers respond by telling you how many times they polled to get that result. The good pollers always perform fewer pollings since there's only one polling for all three listeners, whereas the bad pollers each do their own polling.

Final Thoughts

Macintosh mice often have only one button, so these techniques apply primarily to Windows. However, if you want to detect the right mouse button, be aware that the right-click brings up the Flash Player menu when tested in a browser. The middle button is not assigned a function by default, but since most programs don't use the middle mouse button, many users configure it to do something special, such as minimize all desktop windows. Furthermore, many users don't have three-button mice, so you shouldn't rely on the middle mouse button for important functionality. The bottom line is that this hack remains primarily a curiosity unless you have a dedicated hardware setup, such as a kiosk, where you can control the mouse hardware and software configuration.

Flash Player 7 supports a new TextField.mouseWheelEnabled property to allow the mouse wheel to scroll text fields. Flash Player 7 for Windows also supports a new Mouse.onMovieWheel( ) event listener, which is notified when the mouse wheel moves.