Need to know when the mouse leaves the stage?

Ever need to know when the cursor leaves the stage? This comes up every once in a while and is useful to know. First there is Event.MOUSE_LEAVE which is:

Dispatched by the Stage object when the mouse pointer moves out of the Flash Player window area.

According to Flash Help.

stage.addEventListener( Event.MOUSE_LEAVE, mouseLeaveListener );
 function mouseLeaveListener (e:Event):void {
         trace("mouse left the stage");
         stage.addEventListener( Event.MOUSE_MOVE, mouseMoveListener );
 function mouseMoveListener (e:Event):void {
         trace("mouse re-entered the stage");
         // remove the listener until the next time the mouse exits the stage.
         stage.removeEventListener(Event.MOUSE_MOVE, mouseMoveListener);

Then there is also Event.ACTIVATE and Event.DEACTIVATE. These events are dispatched when Flash gains and loses focus. In other words when the cursors leaves the area of the movie.

A link to a discussion of the subject on Ationscript.orghere

Leave a Reply

Your email address will not be published. Required fields are marked *