Sunday, 15 September 2013

AS3 - KeyboardEvent.KEY_DOWN event not firing in AIR project with preventDefault()

AS3 - KeyboardEvent.KEY_DOWN event not firing in AIR project with
preventDefault()

I am developing an kiosk-like application (a game) which needs to be
locked in full screen all the time. I am using as3/flash/AIR for it.
Things started well at first, and for the most part all works fine.. but
there is a mystery brewing somewhere which I haven't been able to figure
out... That's where your help would be greatly appreciated!
The way I handled this problem is by adding at the very beginning of the app:
stage.addEventListener(KeyboardEvent.KEY_DOWN, playerOnKeyDown);
Then, on my playerOnKeyDown function:
function playerOnKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.ESCAPE)
{
event.preventDefault();
//More code here opening out menus, etc, etc.)
}
}
So, all of this worked just fine, but of course I needed to also bring along:
stage.focus = stage;
into the party, otherwise, when removing objects - as in removeChild() -
the event firing wouldn't behave as I wanted, because flash changed the
focus elsewhere in the display list.
I have been careful to add the focus to the stage every time a remove a
"child", and it works great everywhere, except for one time in the entire
run, right after I remove an object from an externally loaded swf.
I still add the lines as it should be expected to work:
removeChild(ChildFromLoadedSWF);
stage.focus = stage;
except that when I hit the ESC key, it takes me out of full screen (its
default behavior) circumventing completely my listener function
playerOnKeyDown.
The strange thing is that right before doing this, the line:
stage.hasEventListener(KeyboardEvent.KEY_DOWN))
traces true!
The focus is on the stage, the listener is on, and yet when pressing the
ESC key the default behavior is ignoring my function completely....
What could be causing this?
THANK YOU!!

No comments:

Post a Comment