Copy link to clipboard
Copied
So I've got a KeyboardEvent listener added to the stage which properly detects keypresses when the SWF first starts, but when the user presses a button to go to the next frame, it stops working unless they click on the stage first. I read that this is for security reasons, but is there any way around it? I would like to be able to detect keypresses throughout the movie without requiring any clicks.
Copy link to clipboard
Copied
normally, that stage maintains focus unless something is clicked outside the flash stage. if your next-frame button is a flash object, there should be no loss of focus from the stage.
that assumes, of course, that you have correctly assigned your keyboard listener to the stage and not some other object. did you?
Copy link to clipboard
Copied
Upon initially loading a page, you are absolutely required to click a Flash object to send keyboard focus to it. You can't load a new page and expect keyboard focus to be issued to it. What you can do is expect JavaScript to gain the first focus which can relay the keyboard events if you really need this. And yes, this is by design. Your browser is the focus, not flash. Once you click Flash inside a browser, only then can it gain focus.
Copy link to clipboard
Copied
There's no browser involved in this. I'm doing it just from the Flash player. I've stripped it down to just the basics and it still occurs. When the movie starts, pressing any key will trace the keyCode properly, but after credits_btn is clicked and it goes to frame 2, keyboard input doesn't show up until you click on the screen.
//FRAME 1
stop();
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPress);
credits_btn.addEventListener(MouseEvent.CLICK, credits);
function credits(evt:MouseEvent):void {
gotoAndStop(2);
}
function keyPress(evt:KeyboardEvent):void {
trace(evt.keyCode);
if (frameJump > 0) {
gotoAndStop(frameJump);
}
}
var frameJump:int = 0;
----------------------------
//FRAME 2
frameJump = 1;
Copy link to clipboard
Copied
That code works for me. I put the button just on frame 1. I run the test, press keys (traces keyCode), click a button to advance to frame 2, set frameJump=1, then just press keys and it goes back to frame 1 and I see the keycodes being traced the whole time. Here's a short video of me doing that. I didn't record far enough to the bottom of the trace in the video but you can see the scrollbar moving and see the keyCodes being added the whole time, no issue. The recording shows you when I click (the red circles) so you can see keyCodes are being fired just fine without me clicking the Flash Player. The only time I click it is to click the button. I only remove the button on frame 2 to make it obvious a frame jump occurred.
What OS are you running and what version of Flash? I'm in Win64 / CS5.5. There could be focus differences with windows if you're running a Mac.
Copy link to clipboard
Copied
i don't see that problem.
Copy link to clipboard
Copied
That's weird. I'm using Windows 7 64-bit and Flash CS5.5. I'll try it on a school computer and see if the same thing happens.
Copy link to clipboard
Copied
After a bit more Googling, this seems to be what is happening. From this thread:
http://forums.adobe.com/message/1941580
2. badaboom_55, May 5, 2009 6:45 PM in reply to Craig Grummitt Report
I think I figured it out... you were right about the focus being the issue...
Although, I don't think Flash is "losing" focus so much as the object that HAS the focus no long exists.
Which means the problem lies in the tidbit that the KeyboardEvent bubbles up from the focused object through the object's parents to the stage. That is how the listener works even though the stage does not have direct focus. It is also why keyboard listeners are almost always attached to the stage.
So, as I understand the bubbles up thing...
If myPlay_btn has focus, and a key is pressed... the KeyboardEvent will bubble up through the parents until it reaches the stage, where it triggers the listener that was registered with the stage.
However, if myPlay_btn has focus, but no longer exists on the timeline, and a key is pressed... since myPlay_btn isn't on the timeline (display list) it has no parents to bubble up through, and therefore the keyboard event never reaches the stage (which is where the listener would've triggered).
For me this KeyboardEvent listener / stage focus problem only occurs when you click on a button and that button disappears. Adding stage.focus = stage to those button functions fixes things.
Copy link to clipboard
Copied
that's correct, if the object with focus no longer exists, no object in flash has focus. ie, stage.focus will be null.
the problem is even more serious with touch devices using touch events.
Copy link to clipboard
Copied
In my video, the second I click the button it no longer exists. Given your theory, nothing has focus therefore I should no longer be receiving events. But, I do receive events despite it being removed.
And yes you're correct about events bubbling up, that's absolutely necessary if you ever want to do something like target an Input TextField for typing into. However the chances of you removing the object so fast it actually captured and removed an event right before it was removed should be impossible.
Copy link to clipboard
Copied
i've seen the problem in an iOS touch application causing the entire app to loose focus until it was shut down and restarted.
Copy link to clipboard
Copied
Events work a bit different in obj-c and I'm not sure how the change even from that when compiled down into arm code. Sounds like an issue with that compilation more than a Flash issue then. If you can reproduce it on iOS I'd love to know about it myself. It should be submitted for fixing.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now