Top Level

Class Mouse

Object


public class Mouse
extends Object

Player version: Flash Player 5

The Mouse class is a top-level class whose properties and methods you can access without using a constructor. You can use the methods of the Mouse class to hide and show the mouse pointer (cursor) in the SWF file. The mouse pointer is visible by default, but you can hide it and implement a custom pointer that you create using a movie clip . The mouse pointer is visible by default.

A Flash application can only monitor mouse events that occur within its focus. A Flash application cannot detect mouse events in another application.




Property Summary

Properties inherited from class Object
__proto__, __resolve, constructor, prototype


Event Summary
onMouseDown = function() {}
Notified when the mouse is pressed.
onMouseMove = function() {}
Notified when the mouse moves.
onMouseUp = function() {}
Notified when the mouse is released.
onMouseWheel = function([delta:Number], [scrollTarget:String]) {}
Notified when the user rolls the mouse wheel.


Method Summary
static addListener(listener:Object) : Void
Registers an object to receive notifications of the onMouseDown, onMouseMove, onMouseUp, and onMouseWheel listeners.
static hide() : Number
Hides the pointer in a SWF file.
static removeListener(listener:Object) : Boolean
Removes an object that was previously registered with addListener().
static show() : Number
Displays the mouse pointer in a SWF file.

Methods inherited from class Object
addProperty, hasOwnProperty, isPropertyEnumerable, isPrototypeOf, registerClass, toString, unwatch, valueOf, watch


Event Detail

onMouseDown Event Listener

public onMouseDown = function() {}

Player version: Flash Player 6

Notified when the mouse is pressed. To use the onMouseDown listener, you must create a listener object. You can then define a function for onMouseDown and use addListener() to register the listener with the Mouse object, as shown in the following code:
var someListener:Object = new Object();
someListener.onMouseDown = function () { ... };
Mouse.addListener(someListener);

Listeners enable different pieces of code to cooperate because multiple listeners can receive notification about a single event.

Note: This method is only supported in Flash Lite if System.capabilities.hasMouse is true or System.capabilities.hasStylus is true.

A Flash application can only monitor mouse events that occur within its focus. A Flash application cannot detect mouse events in another application.

Example
The following example uses the Drawing API to draw a rectangle whenever the user clicks, drags and releases the mouse at runtime.
this.createEmptyMovieClip("canvas_mc", this.getNextHighestDepth());
var mouseListener:Object = new Object();
mouseListener.onMouseDown = function() {
    this.isDrawing = true;
    this.orig_x = _xmouse;
    this.orig_y = _ymouse;
    this.target_mc = canvas_mc.createEmptyMovieClip("", canvas_mc.getNextHighestDepth());
};
mouseListener.onMouseMove = function() {
    if (this.isDrawing) {
        this.target_mc.clear();
        this.target_mc.lineStyle(1, 0xFF0000, 100);
        this.target_mc.moveTo(this.orig_x, this.orig_y);
        this.target_mc.lineTo(_xmouse, this.orig_y);
        this.target_mc.lineTo(_xmouse, _ymouse);
        this.target_mc.lineTo(this.orig_x, _ymouse);
        this.target_mc.lineTo(this.orig_x, this.orig_y);
    }
    updateAfterEvent();
};
mouseListener.onMouseUp = function() {
    this.isDrawing = false;
};
Mouse.addListener(mouseListener);

The MovieClip.getNextHighestDepth() method used in this example requires Flash Player 7 or later. If your SWF file includes a version 2 component, use the version 2 components DepthManager class instead of the MovieClip.getNextHighestDepth() method.

See also
Mouse.addListener()

onMouseMove Event Listener

public onMouseMove = function() {}

Player version: Flash Player 6

Notified when the mouse moves. To use the onMouseMove listener, you must create a listener object. You can then define a function for onMouseMove and use addListener() to register the listener with the Mouse object, as shown in the following code:
var someListener:Object = new Object();
someListener.onMouseMove = function () { ... };
Mouse.addListener(someListener);

Listeners enable different pieces of code to cooperate because multiple listeners can receive notification about a single event.

Note: This method is only supported in Flash Lite if System.capabilities.hasMouse is true.

A Flash application can only monitor mouse events that occur within its focus. A Flash application cannot detect mouse events in another application.

Example
The following example uses the mouse pointer as a tool to draw lines using onMouseMove and the Drawing API. The user draws a line when they drag the mouse pointer.
this.createEmptyMovieClip("canvas_mc", this.getNextHighestDepth());
var mouseListener:Object = new Object();
mouseListener.onMouseDown = function() {
    this.isDrawing = true;
    canvas_mc.lineStyle(2, 0xFF0000, 100);
    canvas_mc.moveTo(_xmouse, _ymouse);
};
mouseListener.onMouseMove = function() {
    if (this.isDrawing) {
        canvas_mc.lineTo(_xmouse, _ymouse);
    }
    updateAfterEvent();
};
mouseListener.onMouseUp = function() {
    this.isDrawing = false;
};
Mouse.addListener(mouseListener);

The MovieClip.getNextHighestDepth() method used in this example requires Flash Player 7 or later. If your SWF file includes a version 2 component, use the version 2 components DepthManager class instead of the MovieClip.getNextHighestDepth() method.

The following example hides the standard mouse pointer, and sets the x and y positions of the pointer_mc movie clip instance to the x and y pointer position. Create a movie clip and set its Linkage identifier to pointer_id. Add the following ActionScript to Frame 1 of the Timeline:

// to use this script you need a symbol 
// in your library with a Linkage Identifier of "pointer_id".
this.attachMovie("pointer_id", "pointer_mc", this.getNextHighestDepth());
Mouse.hide();
var mouseListener:Object = new Object();
mouseListener.onMouseMove = function() {
    pointer_mc._x = _xmouse;
    pointer_mc._y = _ymouse;
    updateAfterEvent();
};
Mouse.addListener(mouseListener);

The MovieClip.getNextHighestDepth() method used in this example requires Flash Player 7 or later. If your SWF file includes a version 2 component, use the version 2 components DepthManager class instead of the MovieClip.getNextHighestDepth() method.

See also
Mouse.addListener()

onMouseUp Event Listener

public onMouseUp = function() {}

Player version: Flash Player 6

Notified when the mouse is released. To use the onMouseUp listener, you must create a listener object. You can then define a function for onMouseUp and use addListener() to register the listener with the Mouse object, as shown in the following code:
var someListener:Object = new Object();
someListener.onMouseUp = function () { ... };
Mouse.addListener(someListener);

Listeners enable different pieces of code to cooperate because multiple listeners can receive notification about a single event.

Note: This method is only supported in Flash Lite if System.capabilities.hasMouse is true or System.capabilities.hasStylus is true.

A Flash application can only monitor mouse events that occur within its focus. A Flash application cannot detect mouse events in another application.

Example
The following example uses the mouse pointer as a tool to draw lines using onMouseMove and the Drawing API. The user draws a line when they drag the mouse pointer. The user stops drawing the line when they release the mouse button.
this.createEmptyMovieClip("canvas_mc", this.getNextHighestDepth());
var mouseListener:Object = new Object();
mouseListener.onMouseDown = function() {
    this.isDrawing = true;
    canvas_mc.lineStyle(2, 0xFF0000, 100);
    canvas_mc.moveTo(_xmouse, _ymouse);
};
mouseListener.onMouseMove = function() {
    if (this.isDrawing) {
        canvas_mc.lineTo(_xmouse, _ymouse);
    }
    updateAfterEvent();
};
mouseListener.onMouseUp = function() {
    this.isDrawing = false;
};
Mouse.addListener(mouseListener);

The MovieClip.getNextHighestDepth() method used in this example requires Flash Player 7 or later. If your SWF file includes a version 2 component, use the version 2 components DepthManager class instead of the MovieClip.getNextHighestDepth() method.

See also
Mouse.addListener()

onMouseWheel Event Listener

public onMouseWheel = function([delta:Number], [scrollTarget:String]) {}

Player version: Flash Player 6 — (Windows only).

Notified when the user rolls the mouse wheel. To use the onMouseWheel listener, you must create a listener object. You can then define a function for onMouseWheel and use addListener() to register the listener with the Mouse object.

Note: Mouse wheel event listeners are available only in Windows versions of Flash Player.

A Flash application can only monitor mouse events that occur within its focus. A Flash application cannot detect mouse events in another application.

Parameters
delta:Number [optional] — A number indicating how many lines should be scrolled for each notch the user rolls the mouse wheel. A positive delta value indicates an upward scroll; a negative value indicates a downward scroll. Typical values are from 1 to 3; faster scrolling can produce larger values.
scrollTarget:String [optional] — A parameter that indicates the topmost movie clip instance under the mouse pointer when the mouse wheel is rolled. If you want to specify a value for scrollTarget but don't want to specify a value for delta, pass null for delta.

Example
The following example shows how to create a listener object that responds to mouse wheel events. In this example, the x coordinate of a movie clip object named clip_mc changes each time the user rotates the mouse wheel:
var mouseListener:Object = new Object();
mouseListener.onMouseWheel = function(delta) {
    clip_mc._x += delta;
}
Mouse.addListener(mouseListener);

The following example draws a line that rotates when you rotate the mouse wheel. Click the SWF file at runtime and then rotate your mouse wheel to see the movie clip in action.

this.createEmptyMovieClip("line_mc", this.getNextHighestDepth());
line_mc.lineStyle(2, 0xFF0000, 100);
line_mc.moveTo(0, 100);
line_mc.lineTo(0, 0);
line_mc._x = 200;
line_mc._y = 200;

var mouseListener:Object = new Object();
mouseListener.onMouseWheel = function(delta:Number) {
    line_mc._rotation += delta;
};
mouseListener.onMouseDown = function() {
    trace("Down");
};
Mouse.addListener(mouseListener);

The MovieClip.getNextHighestDepth() method used in this example requires Flash Player 7 or later. If your SWF file includes a version 2 component, use the version 2 components DepthManager class instead of the MovieClip.getNextHighestDepth() method.

See also
Mouse.addListener(), TextField.mouseWheelEnabled


Method Detail

addListener Method

public static addListener(listener:Object) : Void

Player version: Flash Player 6

Registers an object to receive notifications of the onMouseDown, onMouseMove, onMouseUp, and onMouseWheel listeners. (The onMouseWheel listener is supported only in Windows.)

The listener parameter should contain an object that has a defined method for at least one of the listeners.

When the mouse is pressed, moved, released, or used to scroll, regardless of the input focus, all listening objects that are registered with this method have their onMouseDown, onMouseMove, onMouseUp, or onMouseWheel method invoked. Multiple objects can listen for mouse notifications. If the listener is already registered, no change occurs.

Note: This method is only supported in Flash Lite if System.capabilities.hasMouse is true or System.capabilities.hasStylus is true.

Parameters
listener:Object — An object.

Example
This example is excerpted from the animation.fla file in the ActionScript samples folder.
// Create a mouse listener object
var mouseListener:Object = new Object();

// Every time the mouse cursor moves within the SWF file, 
    update the position of the crosshair movie clip 
    instance on the Stage.
mouseListener.onMouseMove = function() {
    crosshair_mc._x = _xmouse;
    crosshair_mc._y = _ymouse;
};

// When you click the mouse, check to see if the cursor is within the boundaries of the Stage. If so, increment the number of shots. 
mouseListener.onMouseDown = function() {
    if (bg_mc.hitTest(_xmouse, _ymouse, false)) {
    _global.shots++;
    }
};
Mouse.addListener(mouseListener);

To view the entire script, see the animation.fla file in the ActionScript samples Folder. The following list shows typical paths to the ActionScript samples Folder:

See also
Mouse.onMouseDown, Mouse.onMouseMove, Mouse.onMouseUp, Mouse.onMouseWheel

hide Method

public static hide() : Number

Player version: Flash Player 5

Hides the pointer in a SWF file. The pointer is visible by default.

Returns
Number — An integer; either 0 or 1. If the mouse pointer was hidden before the call to Mouse.hide(), then the return value is 0. If the mouse pointer was visible before the call to Mouse.hide(), then the return value is 1.

Example
The following code hides the standard mouse pointer, and sets the x and y positions of the pointer_mc movie clip instance to the x and y pointer position. Create a movie clip and set its Linkage identifier to pointer_id. Add the following ActionScript to Frame 1 of the Timeline:
// to use this script you need a symbol 
// in your library with a Linkage Identifier of "pointer_id".
this.attachMovie("pointer_id", "pointer_mc", this.getNextHighestDepth());
Mouse.hide();
var mouseListener:Object = new Object();
mouseListener.onMouseMove = function() {
    pointer_mc._x = _xmouse;
    pointer_mc._y = _ymouse;
    updateAfterEvent();
};
Mouse.addListener(mouseListener);

The MovieClip.getNextHighestDepth() method used in this example requires Flash Player 7 or later. If your SWF file includes a version 2 component, use the version 2 components DepthManager class instead of the MovieClip.getNextHighestDepth() method.

See also
Mouse.show(), MovieClip._xmouse, MovieClip._ymouse

removeListener Method

public static removeListener(listener:Object) : Boolean

Player version: Flash Player 6

Removes an object that was previously registered with addListener().

Parameters
listener:Object — An object.

Returns
Boolean — If the listener object is successfully removed, the method returns true; if the listener is not successfully removed (for example, if the listener was not on the Mouse object's listener list), the method returns false.

Example
The following example attaches three buttons to the Stage, and lets the user draw lines in the SWF file at runtime, using the mouse pointer. One button clears all of the lines from the SWF file. The second button removes the mouse listener so the user cannot draw lines. The third button adds the mouse listener after it is removed, so the user can draw lines again. Add the following ActionScript to Frame 1 of the Timeline:
this.createClassObject(mx.controls.Button, "clear_button", this.getNextHighestDepth(), {_x:10, _y:10, label:'clear'});
this.createClassObject(mx.controls.Button, "stopDrawing_button", this.getNextHighestDepth(), {_x:120, _y:10, label:'stop drawing'});
this.createClassObject(mx.controls.Button, "startDrawing_button", this.getNextHighestDepth(), {_x:230, _y:10, label:'start drawing'});
startDrawing_button.enabled = false;
//
this.createEmptyMovieClip("canvas_mc", this.getNextHighestDepth());
var mouseListener:Object = new Object();
mouseListener.onMouseDown = function() {
    this.isDrawing = true;
    canvas_mc.lineStyle(2, 0xFF0000, 100);
    canvas_mc.moveTo(_xmouse, _ymouse);
};
mouseListener.onMouseMove = function() {
    if (this.isDrawing) {
    canvas_mc.lineTo(_xmouse, _ymouse);
    }
    updateAfterEvent();
};
mouseListener.onMouseUp = function() {
    this.isDrawing = false;
};
Mouse.addListener(mouseListener);
var clearListener:Object = new Object();
clearListener.click = function() {
    canvas_mc.clear();
};
clear_button.addEventListener("click", clearListener);
//
var stopDrawingListener:Object = new Object();
stopDrawingListener.click = function(evt:Object) {
    Mouse.removeListener(mouseListener);
    evt.target.enabled = false;
    startDrawing_button.enabled = true;
};
stopDrawing_button.addEventListener("click", stopDrawingListener);
var startDrawingListener:Object = new Object();
startDrawingListener.click = function(evt:Object) {
    Mouse.addListener(mouseListener);
    evt.target.enabled = false;
    stopDrawing_button.enabled = true;
};
startDrawing_button.addEventListener("click", startDrawingListener);

The MovieClip.getNextHighestDepth() method used in this example requires Flash Player 7 or later. If your SWF file includes a version 2 component, use the version 2 components DepthManager class instead of the MovieClip.getNextHighestDepth() method.


show Method

public static show() : Number

Player version: Flash Player 5

Displays the mouse pointer in a SWF file. The pointer is visible by default.

Returns
Number — An integer; either 0 or 1. If the mouse pointer was hidden before the call to Mouse.show(), then the return value is 0. If the mouse pointer was visible before the call to Mouse.show(), then the return value is 1.

Example
The following example attaches a custom cursor from the library when it rolls over a movie clip called my_mc. Give a movie clip in the Library a Linkage identifier of cursor_help_id, and add the following ActionScript to Frame 1 of the Timeline:
my_mc.onRollOver = function() {
    Mouse.hide();
    this.attachMovie("cursor_help_id", "cursor_mc", this.getNextHighestDepth(), {_x:this._xmouse, _y:this._ymouse});
};
my_mc.onMouseMove = function() {
    this.cursor_mc._x = this._xmouse;
    this.cursor_mc._y = this._ymouse;
};
my_mc.onRollOut = function() {
    Mouse.show();
    this.cursor_mc.removeMovieClip();
};

The MovieClip.getNextHighestDepth() method used in this example requires Flash Player 7 or later. If your SWF file includes a version 2 component, use the version 2 components DepthManager class instead of the MovieClip.getNextHighestDepth() method.

See also
Mouse.hide(), MovieClip._xmouse, MovieClip._ymouse