Action Script 3 keyboard event set focus - actionscript-3

I set focus to a text field in the init method, however keyboard enter event only works when I click the text field first.
What is the problem?

It's because, in AS3, by default, stage got the focus. So if you are adding a keyboard event listener to anything other than stage you have to set focus to that object.
Example,
If txt is your text field, you are adding a keyboard event listener to txt then you must do get focus for that like
stage.focus = txt;
That line brought focus to the text field but remember if you click on anything other than that, that object got focus. Needless to say, txt lost focus.
Tip: Try using adding keyboard event listener to stage, if you do so then it dispatches the event when a key is clicked or released regardless of the what object has focus.
stage.addEventListener(KeyBoardEvent.KEY_DOWN, onKeyDown);

Related

Why do I have to click the movie for my character to respond?

My movie has 3 frames, first one is the welcome screen with the play button, and after I press it and jumped to frame2 I have to click the movie for my character/player to respond and move by arrows. Same happens if I go back to frame2 from my game-over screen placed on frame3.
I use gotoAndPlay(); to navigate frames, naturally.
if it's something having to do with my code from frame2 i will post what it is required. I'd like to know why is that happening and how to fix it. THANKS!
Clicking your character gives it focus.
Only* what has focus receives keyboard events.
If you register the listener for the keyboard event on your character object, you have to click it first, before it receives those events.
However, the KeyboardEvents bubble up the display list and eventually reach the top most container which is the stage. This gives you two options:
Handle the focus yourself by assigning the object that should have focus to the stage.focus property. This is basically doing what the clicking does in your current situation.http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Stage.html#focus
register your listeners at the stage
The user simply needs to click anywhere on the flash stage to add keyboard focus. Normally, a good way of achieving this is by using a start button, or something similar, for that first bit of mouse/keyboard focus.

Window not selected after changing frame from using a button

When I change scene with the use of a button in my game it seems to deselect the window. Then it does not respond to keyboard input without clicking the window again afterwards.
Is there a way to stop this with some sort of command or a way to bypass this with a different method.
When you click an object that object gains focus. During your buttons MouseEvent.CLICK event listener, you need to call the setFocus() function on the object you need the key strokes on.

Actionscript UIScrollbar and Click Event on containing Element

I'm creating a game in Flash CS5.5, basically a version of "Who wants to be a millionaire?".
I have an answerDisplay Class which contains an answer text. Answer texts can become rather long, and since I have limited screen space I check for very long texts and if a text is too big I add a UIScrollbar besides the text field.
Which works nicely, except for one thing - when the user drags the scroll tab, the text scrolls fine. When the user clicks one of the scrollbar arrows the text field also scrolls fine, as long as the button is clicked, but as soon as the mouse button is released the click event of the containing answerDisplay class is fired, which leads to my program wrongly thinking the user had selected the answer while he just tried to scroll.
I tried catching this via e.target and/or e.currenttarget, but it's no use, I never get a reference to the scrollbar, so I'm never sure if the user clicked one of the arrows or if he genuinely clicked the answer to select it.
How can I make certain that the click event of my answerDisplay Class is not fired when the UIScrollbar is used?
Checking e.target and e.currentTarget is a good start.
You might also want to check the event phase:
For more details on event phase, check grapefrukt's answer
A click from the answerDisplay Class dispatched event should have AT_TARGET phase, while the UIScrollbar event should have the BUBBLING_PHASE. This should help you differentiate between the two.
Also, you could add event listener to the UIScrollbar in your answerDisplay Class to capture the event and then suppress it via stopPropagation().
Depending on how UIScrollbar uses event bubbles internally, you can try either stopPropagation(), either stopImmediatePropagation(). The goal is to stop the event bubbling up from answerDisplay, but not breaking the UIScrollbar functionality.

How can I press a swing JButton using mouse events?

I Have a JButton (or any Component, really) that I would like to trick into thinking that it has been clicked on and therefore firing its action performed event. I don't want to call the action performed or doClick directly because I am trying to make a solution that generalizes beyond buttons, and I am constrained to posting events.
I would have thought that posting a MousePressed event to the system event queue would have done it, but no. ( with appropriate location, click count etc.)
Then I thought a mouse entered followed by a mouse move, a mouse pressed, a mouse released, and a mouse clicked, but no.
None of this causes the JButton to fire its action performed like it does when I actually click on it.
Has anyone caused a JButton to spit out its action performed event by driving it with mouse events? Moved a JSlider with mouse events? Expanded a tree node?
Thanks for any help.
Have you tried the fireActionPerformed method? I can't off the top of my head now remember if all components have it but if i remember right, all buttons and menu items should deifnately have it. Just a thought

KeyUp event of one button navigating to Enter event of other button

I have a keyUp event in button1 and EnterEvent in button2
When i press button1 and use my up arrow automatically control is navigating to Enter Event of button2 after entering into the KeyUp event of button1
Feels something fishy; Please help !!
Just to be clear, the KeyUp event doesn't refer specifically to the Up key on your keyboard. It is an event that triggers anytime you release any key. The keyboard events are KeyDown (when you push any key down), KeyPress (after KeyDown), and KeyUp (when you let go of the key). If you hold down a key, the KeyDown and KeyPress events trigger repeatedly until you let go, at which point KeyUp fires. (Note: pressing the Enter key on a control that is set as Default or pressing the Esc key on a control that is set as Cancel will NOT trigger any of the Key events for those controls.)
Also, the Enter event doesn't refer to the Enter key, it refers to anytime you enter that control, whether by clicking your mouse into it or moving to it via the keyboard.
In light of all of this, here's what looks to be happening:
You press and release the Up Arrow on your button1, triggering button1's KeyUp event. The focus then moves to button2 (because you pressed the Up arrow key, a navigation key) and triggers the Enter event of button2 (because you just entered button2).
From MSDN
"The Enter event occurs before a control actually receives the focus from a control on the same form."
sounds like its doing the right thing, when you press the up arrow focus is being switched to the next button (Button2) which is causing the Enter event to fire.