PDA

View Full Version : Capturing global mouse clicks


fross
03-02-2007, 04:33 PM
Hi all,

I have a question on how to do something that I hoped was simple, but is getting complicated, so I thought maybe I'm doing something wrong.

I want to have a TextField, that when clicked on, pops up a window (a Sprite containing more TextFields, basically) that gives the user more options. And when the user clicks outside of this popup area, it disappears.

I can get it almost working as such quite easily - I can get the popup appearing, and the links working, and clicking on the original text removes the popup. Almost perfect.

But it's this capturing the "any click outside the box" (but inside the movie obviously) that is proving difficult. I tried doing stage.addEventListener for MouseEvent.CLICK when opening the popup, but that just triggers immediately. Plus, I'm not sure whether it would be the best thing, it might override the link in the popup I do want to work?

This should be something pretty basic to do (I am in pure AS3, so no nice Flex stuff to do it for me!), but I'm sure someone has done this already, so any hints you can give would be very welcome.

Thanks, and hope you all have a good weekend.

dr_zeus
03-02-2007, 06:21 PM
I tried doing stage.addEventListener for MouseEvent.CLICK when opening the popup, but that just triggers immediately.

Can you explain what you mean by it triggering immediately?

You're on the right track by using the stage, and you should continue trying to get it to work. My guess is that the event is bubbling and you need to check the event's target property to be sure you want to use a particular event. Alternatively, you may need to start listening to the stage at a later point in time so that you don't get this immediate triggering.

Tink
03-02-2007, 06:23 PM
yeah it should fire immediately, as the click as already happened. The prob with listening to stage, is that this will pick up click on the actually popup as well.

You may be best off just adding an invisible button to the background and listeneing to that.

dr_zeus
03-02-2007, 09:15 PM
To get around clicks on the popup, you can check the target.

private function myStageClickHandler(event:MouseEvent):void
{
if(event.target == myPopUp || myPopUp.contains(event.target))
return;

//do other stuff.
}

fross
03-05-2007, 09:57 AM
Thanks for the replies guys :)

To explain the "triggers immediately", if i step through the code in the debugger, then my event handler for a mouseclick gets triggered again immediately, once i add an event listener to the stage. i suspect it is a bubbling issue, but i never really got the hang of that so i will try to avoid it for now :)

i think i'll go with tink's suggestion of a giant invisible button, that's probably the simplest way to go. thanks!