tags:

views:

194

answers:

4

What's the correct way to detect, from Flash, when someone has started a drag within the browser (eg, a MOUSE_DOWN event), dragged the mouse outside the browser window, released the button, then moved the mouse back over the browser?

For example (assuming StackOverflow was a Flash application):

alt text

I've tried the "obvious" thing, checking event.buttonDown in the MOUSE_MOVE handler, but even though the mouse button is up, event.buttonDown is true in step 2 (above).

So, is there any other way to check the "real" status of the mouse button? Or any other way to handle this situation?

A: 

For the case where the mouse is leaving your work area, try: MouseEvent.MOUSE_OUT

You may be able to detect MOUSE_UP in the case where the mouse is dragged back in.

The behavior of the mouse outside of your flash app is entirely operating system and browser dependent, and any access you have to it within flash will be inconsistent at best.

Aaron H.
Unfortunately `MOUSE_UP` doesn't fire when the mouse is moved back in. And, yes — the mouse behavior outside of Flash is system-dependent… But I care about what happens when it gets back into Flash, which is Flash dependent.
David Wolever
So I'm not entirely right — `MOUSE_UP` does fire. But only on some browsers. See comments on jpop's answer.
David Wolever
+1  A: 

Check out this post and see if this helps.

http://stackoverflow.com/questions/1563434/detect-mouse-leave-stage-while-dragging-in-actionscript-3

mswallace
+1  A: 

After doing a bit of digging in the Flex source, it seems like they use the SandboxMouseEvent.MOUSE_UP_SOMEWHERE event. Some limited testing suggests that this is one way of correctly detecting mouse-ups outside of Flash (albeit using Flex):

var sbroot:DisplayObject = Application.application.systemManager.getSandboxRoot();
sbroot.addEventListener(SandboxMouseEvent.MOUSE_UP_SOMEWHERE, handleDragComplete);
David Wolever
NB: See the comments on jpop's answer, below. Apparently the `MOUSE_UP` event is fired by some browsers but not others (however, as far as I can tell `MOUSE_UP_SOMEWHERE` works everywhere).
David Wolever
+1  A: 

I had a similar problem. My solution:

In the draggable object constructor I have:

addEventListener(MouseEvent.MOUSE_DOWN, onStartDrag);

And in the onStartDrag() I put:

stage.addEventListener(MouseEvent.MOUSE_MOVE,onDrag);
stage.addEventListener(MouseEvent.MOUSE_UP, onDrop);

You get a notification whenever the mouse moves, in our outside of the flash player, and a MOUSE_UP notification as soon as the button is released, also regardless of the mouse pointer position. Might not be applicable in your particular situation, but works like a charm for me. Don't forget to remove MOVE and UP listeners in the onDrop().

jpop
Unfortunately this isn't true — you don't get a `MOUSE_UP` event if the mouse is released outside the flash player.
David Wolever
Well, I beg to differ. Click on http://www.ewhent.com/Content/Player.aspx?t=1434 and you can see the exact code included above (actually copied directly from the project source) in action, working exactly as I described it. At least, it does for me, on my 10,0,22,87 flash player on Windows. You get a different behaviour?
jpop
Ah, very strange. It appears that Safari on OS X *does* trigger the `MOUSE_UP` outside the stage, but Firefox on OS X *doesn't*. However, Both FF and Safari appear to trigger the `MOUSE_UP_SOMEWHERE` event (see my answer). Grumble Grumble. Anyway, thanks for setting me straight.
David Wolever