- Home
- Articles
- Best Practices
- Keyboard events on stage are not working... why?
Keyboard events on stage are not working... why?

Tecsi Aron
Been working in IT since 2000, mostly in VB* till 3 years ago, when i migrated to Flash and AS3. Made several projects that combine AS3 with PHP. Mostly worked on games and Desktop applications.
View all articles by Tecsi AronYou are making a pac-man game, you want your user to have some control over the game so you place a menu button, you write the code for your menu. After you did this you notice that you no longer can control your pacman. After some headache you find out that if you click anything (except the menu button), your games works again. More technically speaking after doing removeChild to remove your menu your KeyboardEvent.KEY_DOWN/UP added to stage isn't firring any more. Example:
Just as described above you will see that the label at the top of the swf indicates correctly if a button is pressed or not UNTIL I remove the menu. Code is as follows:
var mainM:MovieClip;
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyD);
stage.addEventListener(KeyboardEvent.KEY_UP,keyU);
btn.addEventListener(MouseEvent.CLICK,makeM);
function keyD(e:Event)
{
textField.text="Key pressed";
}
function keyU(e:Event)
{
textField.text="Key not pressed";
}
function makeM(e:Event)
{
if (mainM==null)
{
mainM=new menu();
addChild(mainM)
mainM.x=0;
mainM.y=60;
}
}
function destroyM(e:Event)//function called from mainM that tells the SWF that the menu needs to close.
{
removeChild(mainM)
mainM=null;
}
"menu" is in my library and has Export for ActionScript.
So here's the part most people don't figure out (because its not really rational): though i did remove the menu from the stage, and eliminated any reference to it, since i clicked on the "Close Me" button it got focus, and it kept the focus even after being removed. (This is the irrational part... why would you want the focus on something that you cant interact with.). So let's recap, stage.addEventListener(KeyboardEvent.KEY_DOWN,example) is supposed to bubble to the stage, but the item that has focus, and there for is getting the event is no longer a child of the stage, this means the event will never bubble up back to the stage.
The solution for this long and stupid problem is one tiny line:
stage.focus=anyMCOnStage;
In my case all i have to do is to add this line:
stage.focus=stage;
to my destroyM function. The result being:
Hope someone will find this helpful.
Best regards.
Spread The Word
17 Responses to "Keyboard events on stage are not working... why?" 
|
said this on 07 Jul 2009 3:28:13 AM CST
Nice one. Simple issue, r
|
|
said this on 17 Jul 2009 12:08:00 AM CST
good - just came across s
|
|
said this on 28 Jul 2009 7:51:40 AM CST
Been driving me crazy thi
|
|
said this on 01 Dec 2009 6:53:03 AM CST
realy strange noise. but
|
|
said this on 08 Mar 2010 12:34:58 PM CST
You saved my butt, sir. I
|
|
said this on 01 Apr 2010 9:17:57 PM CST
This same problem gave me
|
|
said this on 27 May 2010 2:01:02 AM CST
Thanks, my problem iss to
|
|
said this on 28 May 2010 8:35:14 AM CST
Really thanks man Your ti
|
|
said this on 09 Jun 2010 6:58:11 AM CST
Thanks so much - been tea
|
|
said this on 16 Jun 2010 9:11:40 AM CST
Thanks so much again and
|
|
said this on 21 Jun 2010 6:51:37 PM CST
Thank you so much! This
|
|
said this on 04 Jul 2010 7:20:47 AM CST
tthhaannkk YYOOUU :) :) :
|
|
said this on 04 Aug 2010 5:11:44 AM CST
thanks a lot,it really he
|
|
said this on 08 Aug 2010 8:04:36 PM CST
thank you. it's very help
|
|
said this on 28 Feb 2011 7:43:36 AM CST
really help, thanks!
|
|
said this on 08 Jul 2011 8:07:29 AM CST
thanks a lot! you saved m
|


Author/Admin)
