Subscribe

  • Subscribe  

Detecting a mouse up outside event in AS3

Posted by Jay | May 11, 2008 .

In ActionScript3 event driven programming plays a major part with mouse or key pressed and even a text field having events associated with them. Events indicate action e.g. mouse press corresponds to “mouse down” event and vice versa is “mouse up” event.

To check if mouse release (mouseUp -Event.MOUSE_UP)) has happened on the target object on which mouse down – Event.MOUSE_DOWN took place or a “mouse up outside “event when mouse up has not happened on the target object; there is no predefined event with AS3 unlike its earlier versions that had “onReleaseOutside event”.

To resolve this, we need to add stage event handler for mouse up in mouse down event handler. By adding stage event handler for mouse up will detect any global mouse up event. Hence it will check if the event associates the target object that has mouse down event earlier or it was a mouse up outside event not related with the target object.

Package {
  	 Import flash.display.DisplayObject;
  	 import flash.events.*;

	 public var child: DisplayObject = new DisplayObject ();
	 public function childdown(event:MouseEvent):void 
		{  
		 // mouse up event happens only when there is mouse down
		 trace("child mouse down");
		 stage.addEventListener(MouseEvent.MOUSE_UP,childUpOutside);
		}

	Public function childUpOutside (event:MouseEvent):void 
	{
		 if (event.target != child) 
		 {
 			  trace("child mouse up outside");
		 }
	   stage.removeEventListener(MouseEvent.MOUSE_UP, childUpOutside);
	}
}


Leave a Comment

If you would like to make a comment, please fill out the form below.


Name

Email

Website

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Comments

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word


Related Posts

8 Comments so far
  1. Campbell  May 11, 2008 1:20 pm

    Have you seen:

    FlexMouseEvent.MOUSE_DOWN_OUTSIDE

    😉

    Campbell

  2. Flexy  May 27, 2008 10:31 am

    A quick comment to stop newbie AS3.0 devs being misled:
    FlexMouseEvent is only any good if you’re building apps that leverage the Flex Framework. This article discusses AS3.0, with no specific reference to Flex. The example above offers the only solution available to detecting MOUSE_UP events outside of the boundary of the DisplayObject that received the MOUSE_DOWN event.

  3. zack  June 3, 2008 6:59 am

    what about mouse up outside the actual flash movie. how would you capture that?

  4. Anonymous  July 30, 2008 8:30 am

    Correct me if I’m wrong but the above will not work in flash. From what I understand, event.target always returns the dispatcher, since the stage is the dispatcher of the event the target will always be the stage. But, I am a flash guy and maybe this model is different in flex.

  5. hristo  January 13, 2009 1:37 pm

    You could also use FlexMouseEvent.MOUSE_DOWN_OUTSIDE

  6. Anonymous  March 10, 2010 6:32 pm

    addEventListener(FlexMouseEvent.MOUSE_DOWN_OUTSIDE, ListDropMenu_MouseDownOutside, false, 0, true);

  7. meena  April 21, 2011 10:06 pm

    this is wrost why u put like this kind of topics waste fellows

  8. Anonymous  December 18, 2013 5:52 am

    this method is not run perfectly guys……

<

Error. Page cannot be displayed. Please contact your service provider for more details. (9)


Direct TV Offers - usdirect has the best directtv deals