Posts Tagged ‘Focus’

Focus on key listeners

Posted in Flash on August 31st, 2009 by Knut – Be the first to comment

Being a master at getting odd bugs that nobody else gets I just discovered a little issue with adding and removing KeyboardEvents to an application.

My app has a series of forms arranged in steps. The design of one of the forms required keyboard input to be useful. Every form section is added to stage, showed, verified submitted, hidden and then removed from stage before the next one is displayed.

Two steps into this sequence I required key input and could not get it to work without clicking somewhere first. I first thought it was the old focus security fix that was playing up but since I had already clicked a button to show the form I knew the app had focus.

I found that the stage.focus property was set to the last button clicked. This button was the “next” button in the previous form. Since this button no longer was on stage its button events obviously could not bubble to stage.  Doh! So when setting key listeners: always set focus to the item you want to listen to. Or at least something that’s on stage.

stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler);
stage.focus = stage;