Tip: Detecting when mouse leaves stage

I noticed a comment someone made somewhere (Flashkit I think) about how in the Box2D Flash demo it doesn't detect when the mouse leaves the window (ie keeps trying to pull the mouse joint). Skatehead said he wasn't sure if it was possible and for someone to let him know if they knew how to do it. So here it is:

Code:
addEventListener(Event.MOUSE_LEAVE, mouseLeave);


Obviously you need a function "mouseLeave" or whatever you want to call it to tell it to do whatever. This is only available in AS3 though, so actually until somewhat recently there was no way to do it. I ran into this while creating a custom cursor class and it works very well. One thing I did get hung up on though was that I read about it online and then when I tried to use it I used "MouseEvent.MOUSE_LEAVE" which Flex said didn't exist! I finally realized it is not a MouseEvent (strangely enough since it directly concerns...uh the mouse!). I could see it going either way since it's related to the state of the Flash app, but I really would have gone with MouseEvent... So anyhow that's how it works! I would love to see this used more by people since I'm tired of seeing custom cursors stick on the edge of the window. Also it's nice to deactivate samples on blogs and stuff so it doesn't hog processor.
Another useful event is Event.DEACTIVATE that is dispatched on the stage which indicates that the app has lost system/browser focus. :)
Yes that is an important one too! I think it is more commonly known though, and I believe it was introduced before AS3. I actually had a weird bug with my keypress handling code where it would continue to hold down a key if you were holding it down when you clicked outside the window. I previously used DEACTIVATE to detect this and it fixed it. They are both very handy and similar events, with subtle differences.
Thanks brainclog, didn't think to look in the Event class. (:

Original thread: https://box2d.org/forum/viewtopic.php?t=363