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.
Related
I have made custom Class for Input TextField with caption and own virtual keyboard which is loading in same MovieClip where TextField resides. And all works just fine. I wanted to remove keyboard from stage (from MovieClip stage) when user clicks on other visible input TextField, where new keyboard will be displayed.
If I check losing focus from MovieClip, wherever I click on keyboard (but not into Input TextField), it is closed because stage loose focus. Is there any way to check if any of children has no focus, so I can in that moment close the keyboard? In other word, to check if focus completely moved out from MovieClip stage to another.
I suppose you're just looking for FocusEvent In case you're need to check focus on MovieClip's children, you're have to attach listener to all of them.
I have a document class with some constructor code. I have two event listeners in the constructor. One of them is an enter frame listener and the other is a mouse click listener. The enter frame function moves a rectangle on the stage. The mouse event function removes it from the stage when its been clicked.
When I used the following:
addEventListener(MouseEvent.CLICK, checkTarget);
The rectangle didn't get removed. But when I used:
stage.addEventListener(MouseEvent.CLICK, checkTarget);
It worked fine. I kept the enter frame without referencing the stage and it worked.
addEventListener(Event.ENTER_FRAME,update);
Why did the mouse click need the reference to the stage and the enter frame didn't? Aren't they both added to the same thing at the same time?
mouse event needs something to work with but ENTER_FRAME is built to work with stage.
I would change the mouse event listener to listen to when you clicked at a mc just like that rectangle if I where you.
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)
I'm an as3 newbie. I'm experiencing this strange problem. I've created a button (type: MyButton) with two child, a text (TextField) and a image icon (type: MyIcon).
Then I've append an eventlistener mouse_click on my button.
As soon as I click on the text, the e.target on the handeler function is recognized of MyButton type. Otherwise, if I click on the image icon (child of button) the e.target is MyIcon type, instead of MyButton.
How can I prevent this? I need all click to be recorded on the button, where I've stored some attributes I need on handeler function.
Thanks.
Use the e.currentTarget instead. It returns the object that the MouseEvent has currently bubbled to. e.target returns the object that the MouseEvent actually started on.
Read about Event bubbling here. It's a very important concept to grasp.
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.