Search Flex Components Free

Custom Search

December 27, 2007

AS3 Mouse Events and Mouse Related User Actions

Flash is great for monitoring all actions by a user from what they type to what their mouse is doing and for how long. We have many tracking systems that rely on script in Flash and HTML when not using flash that can detect user interaction with our applications. Many tracking companies also use this info for hotspot or heat maps to see what your users are messing with.To do all this interaction logging and tracking you need mouse events. Mouse events drive games, applications, cool user controlled interfaces, expected user direction and simplify your application. Understanding all the mouse events possible is important. Here is a list of all the events that are clearly implemented in AS3:CLICK : String = “click” MouseEvent
Used to detect mouse clicks. DOUBLE_CLICK : String = “doubleClick” MouseEvent
Used to detect double clicks.MOUSE_DOWN : String = “mouseDown” MouseEvent
Checks when mouse is pressed down.MOUSE_LEAVE : String = “mouseLeave” Event
Monitors when the mouse leaves the stage. MOUSE_MOVE : String = “mouseMove”
Monitors when the mouse moves.MOUSE_OUT : String = “mouseOut”
Monitors when the mouse moves out of the attached to object of the event.MOUSE_OVER : String = “mouseOver”
Monitors when the mouse moves over the attached to object of the event.MOUSE_UP : String = “mouseUp”
Monitors when the mouse moves up the attached to object of the event from a click.MOUSE_WHEEL : String = “mouseWheel”
Monitors when the mouse wheel moves, detect the positive or negative delta property for distance and direction moved.Wiring up Events in AS3 is easier than its ever been. // attach the event listener to this object, if you want a global event outside
// the current class attach to stage.addEventListener([event],[callback])

this.addEventListener(MouseEvent.CLICK, onMouseClickEvent);
// then make the callback
public function onMouseClickEvent(event:Event) {
// if primary button down, left mouse button
trace(”left button was down”);
trace(”left button was not down”);
}What about right clicks?Well not all mouse equipment has right click so its not always best to implement but for the ones that have right click capabilities and other buttons there is both mouse wheel and right click support in Flash mouse events.To detect right click:In the MouseClick event there is a buttonDown property on the event that returns true if its a left click, false if its any other mouse button. So you could have a menu drop down on a mousewheel click, right mouse click or other mouse button clicks.Detecting right clicks or other mouse clicks is impossible in Flash AS3. I got excited and error in testing and thought the buttonDown helped to determine the button pressed but it only listens to the left click. What about drag over and drag out?The buttonDown property is mainly used for drag over or drag out events (which are not an actual event) but you could do something like:

function onMouseOver(event:MouseEvent):void {
if(event.buttonDown==true) {

// mouse is down and dragged over.
}function onMouseOut(event:MouseEvent):void {
if(event.buttonDown==true) {

// mouse is down and dragged over.

To detect when the mouse leaves the screen:Use the MouseEvent.MOUSE_LEAVE event on your main document class or a class instantiated that assigns this event to the stage. This can be used for high intensity flash sites where performance is preserved when the user is not interacting with certain elements.

More on mouse events and timers shortly. Here’s a sample from the docs:

package {
import flash.display.Sprite;
public class MouseEventExample extends Sprite {
private var size:uint = 100;
private var bgColor:uint = 0xFFCC00;

public function MouseEventExample() {

var child:ChildSprite = new ChildSprite();

import flash.display.Sprite;

class ChildSprite extends Sprite {
private var size:uint = 50;
private var overSize:uint = 60;
private var backgroundColor:uint = 0xFFCC00;
private var overColor:uint = 0xCCFF00;
private var downColor:uint = 0×00CCFF;
public function ChildSprite() {
draw(size, size, backgroundColor);
addEventListener(MouseEvent.CLICK, clickHandler);
addEventListener(MouseEvent.DOUBLE_CLICK, doubleClickHandler);
addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);

addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);

addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);

addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);

addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheelHandler);
private function draw(w:uint, h:uint, bgColor:uint):void {


graphics.drawRect(0, 0, w, h);
private function clickHandler(event:MouseEvent):void {

private function doubleClickHandler(event:MouseEvent):void {

private function mouseDownHandler(event:MouseEvent):void {

draw(overSize, overSize, downColor);
var sprite:Sprite = Sprite(;
sprite.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);

private function mouseMoveHandler(event:MouseEvent):void {

private function mouseOutHandler(event:MouseEvent):void {
draw(size, size, backgroundColor);
private function mouseOverHandler(event:MouseEvent):void {
trace(”mouseOverHandler”);draw(overSize, overSize, overColor);

private function mouseWheelHandler(event:MouseEvent):void {
trace(”mouseWheelHandler delta: ” +;


private function mouseUpHandler(event:MouseEvent):void {

var sprite:Sprite = Sprite(;
sprite.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);

draw(overSize, overSize, overColor);

Related Flex Tutorials