# KeyboardEvent.KEY\_DOWN in AIR project does not trigger handler with preventDefault()

**URL:** <https://forum.kirupa.com/t/keyboardevent-key-down-in-air-project-does-not-trigger-handler-with-preventdefault/331641>\
**Category:** flash\
**Created:** [September 16, 2013, 12:33am UTC](https://forum.kirupa.com/t/keyboardevent-key-down-in-air-project-does-not-trigger-handler-with-preventdefault/331641 "2013-09-16T00:33:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leo\_Perez](https://avatars.discourse-cdn.com/v4/letter/l/97f17d/32.png) [@Leo\_Perez](https://forum.kirupa.com/u/Leo_Perez)\
**Post date:** [September 16, 2013, 12:33am UTC](https://forum.kirupa.com/t/keyboardevent-key-down-in-air-project-does-not-trigger-handler-with-preventdefault/331641/1 "2013-09-16T00:33:51Z")

</div>

[COLOR=#000000][FONT=Arial]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![/FONT][/COLOR]  
[COLOR=#000000][FONT=Arial]The way I handled this problem is by adding at the very beginning of the app:[/FONT][/COLOR]

```auto
[FONT=courier new]stage.addEventListener([COLOR=#2B91AF]KeyboardEvent[/COLOR].KEY_DOWN, playerOnKeyDown);[COLOR=#000000]Then, on my playerOnKeyDown function:[/COLOR]
[COLOR=#00008B]function[/COLOR] playerOnKeyDown([COLOR=#00008B]event[/COLOR]:[COLOR=#2B91AF]KeyboardEvent[/COLOR]):[COLOR=#00008B]void[/COLOR]
{
    [COLOR=#00008B] if[/COLOR] ([COLOR=#00008B]event[/COLOR].keyCode == [COLOR=#2B91AF]Keyboard[/COLOR].ESCAPE)
    {
        [COLOR=#00008B] event[/COLOR].preventDefault();
        [COLOR=gray] //More code here opening out menus, etc, etc.)[/COLOR]
    }
}[/FONT]

```

[COLOR=#000000][FONT=Arial]  
So, all of this worked just fine, but of course I needed to also bring along:[/FONT][/COLOR]  
stage.focus = stage;[COLOR=#000000][FONT=Arial]into the party, otherwise, when removing objects - as in [FONT=courier new]removeChild() [/FONT]- the event firing wouldn’t behave as I wanted, because flash changed the focus elsewhere in the display list.[/FONT][/COLOR]  
[COLOR=#000000][FONT=Arial]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.[/FONT][/COLOR]  
[COLOR=#000000][FONT=Arial]I still add the lines as it should be expected to work:  
[/FONT][/COLOR]

```auto
[FONT=courier new]removeChild(childFromLoadedSWF);
stage.focus = stage;[/FONT]

```

[COLOR=#000000][FONT=Arial]Except that when I hit the ESC key, it takes me out of full screen (its default behavior) circumventing completely my listener function [FONT=courier new]playerOnKeyDown.[/FONT][/FONT][/COLOR]  
[COLOR=#000000][FONT=Arial]  
The strange thing is that right before doing this, the line:[/FONT][/COLOR]

```auto
[FONT=courier new]stage.hasEventListener([COLOR=#2B91AF]KeyboardEvent[/COLOR].KEY_DOWN))[/FONT] // [COLOR=#000000][FONT=Arial]traces true![/FONT][/COLOR]

```

[COLOR=#000000][FONT=Arial]  
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…[/FONT][/COLOR]  
[COLOR=#000000][FONT=Arial]What could be causing this?[/FONT][/COLOR]  
[COLOR=#000000][FONT=Arial]THANK YOU!![/FONT][/COLOR]
