When I add a keypress event listener to the stage, it does not fire when the focus is on another object. How can I listen for any keypress event regardless of the focus?
Just setting the useCapture parameter to true does not work . I think it's because no event is bubbled up if I do not add a listener to every object on the stage.
Do I really have to add an eventlistener to every object to capture a keypress event regardless of the current focused object?
I think if you add your keyEventListener to the stage and then in functions where the focus is changed add the code:
stage.focus = stage
it might work.
Related
In an Actionscript 3 program I used this code to add an event listener to a TextField:
var tf:TextField = new TextField();
tf.addEventListener(KeyboardEvent.KEY_DOWN, handleText);
handleText() only gets called if I click on the TextField. Is there a way to receive keyboard events when the mouse is just hovering over the TextField (no need to click)?
A TextField has its own keyboard event listener that it uses to alter text, if its mode is input. And to capture keyboard events while your target does not have focus, add the listener to stage, and check if your mouse is over the text field in question. For this, you can set a boolean flag in MOUSE_OVER and drop it in MOUSE_OUT listener attached to the textfield. Then, if the flag is on, you can either parse the keyboard event yourself or transfer it to the TextField via dispatchEvent() call with existing KeyboardEvent object.
I would like to know if the mouse button is down, even if another object is being clicked. How do I do that? Simply adding event listeners doesn't work as it does not trigger if something else is on top of the object.
Thanks
Add both a Click & MouseDown event listeners to the stage.
If an object is clicked , the event will bubble up to the stage so you should be able to register it and react accordingly.
You can also check the event.currentTarget property to find out where the event originated from , this should tell you if it was the stage or an object being clicked as well as where the mouse down event came from.
Add a mouse down event listener to the stage (or other parent of all objects in question)
What is the difference between event.bubbles to false for any event, and setting event.stopPropagation() or stopImmediatePropagation() while handling event?
I'm using Flex4 with AS3.
Information found at this article - Introduction to event handling in ActionScript 3.0 is more demonstrative and easy to understand. It will enhance the above accepted answer by #Jason Sturges.
Event bubbling and event capturing are two faces of events. If you make the event.bubbles to false that means the event is marked as non-bubbling event.
bubbles: Indicates whether or not the event is an event that bubbles (and captures). This does not mean that the event went through or is going through a capture or bubbles phase, but rather it is a kind of event that can.
Below image (from the above article) shows how the event goes through the process.
The difference of the stopPropagation() and stopImmediatePropagation() will be more clear in following images.
StopPropagation :
StopImmidiatePropagation :
Setting bubbles to false means the event does not bubble up the display list at all.
stopPropagation() and stopImmediatePropagation() make the current event listener the last to process an event.
The difference between stopPropagation() and stopImmediatePropagation() is that stopImmediatePropagation() will not only prevent the event from moving to the next node, but it will also prevent any other listeners on that node from capturing their events.
Look at the example:
object.addEventListener( MouseEvent.CLICK, functionOne );
object.addEventListener( MouseEvent.CLICK, functionTwo );
If functionOne contains event.stopPropagation(), functionTwo will be called as well.
If it contains event.stopImmediatePropagation(), functionTwo will be ignored.
When I add a keypress eventlistener to the stage, it does not fire when the focus is on an other object ... How can I listen for any keypress event regardless of the focus?
try to hang keyboard event on stage
Is there a way to check the method that has been attached to the stage?
I have stage as global.. and need to fire some function in a object on mouseup...
Now it fires 2 or 3 depending how many objects i add..
I need something like..
if($.stage.hasEventListener(MouseEvent.MOUSE_UP, this.mouseUp) === false){
$.stage.addEventListener(MouseEvent.MOUSE_UP, this.mouseUp);
}
Or a better way to handle this?
I'm guessing you are adding the listener inside each object, no? That means every time you create an instance of your object you are adding yet another listener for stage mouse up events. If you really only want a single listener for this type of event, move it outside of the scope of the object and only add the listener once. Good luck!