Flash: Correctly handling click-and-drag outside the browser?
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 http://img.skitch.com/20100531-t99mni7s8sjs8ycqe5ebyaxphm.png
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 ch开发者_StackOverflow社区eck the "real" status of the mouse button? Or any other way to handle this situation?
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);
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().
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.
精彩评论