Disable ESC in Fullscreen on Adobe AIR

Disable ESC in Fullscreen on Adobe AIR
One of the issues with building a desktop application with Adobe AIR is that there is a built in function in the runtime that will cancel any full screen mode when the user pushes the Escape key. This is mostly a functionality of Flash, but it carries over to AIR since AIR is pretty much an extension of the Flash runtime.

Fortunately when you're using the AIR runtime you can actually override the default functionality of the Escape key and keep your application full screen when the user pushes Escape. This came up in Offspring Fling because I was using the Escape key in my UI design. The user pushes Escape to return to the main menu, pause the game, or skip cutscenes, so it was a problem when escape also canceled the game's full screen mode.

The fix for this is super easy and straight forward. First you just need an event listener on the stage for the KEY_DOWN KeyboardEvent.
//listen for key down event and trigger escOverride function
FP.stage.addEventListener(KeyboardEvent.KEY_DOWN, escOverride);
Now here's what the escOverride function looks like.
private function escOverride(e:KeyboardEvent):void {
if (e.keyCode == 27) {
e.preventDefault();
}
}

The keyCode for the Escape key is 27, so whenever a key is pressed the function escOverride is called. If the keyCode was 27, then the preventDefault() function will prevent the default action from being triggered. In this case, it's the "exit fullscreen" action.

There you have it! With just 4 or 5 lines of code total you can prevent your Adobe AIR game from canceling fullscreen when the user pushes Escape. With great power comes great responsibility. (Also note that I believe this is impossible in a normal swf build. The preventDefault() function only works in Adobe AIR.)

Comments

Fedyfausto
Fedyfausto
thx a lot Kyle :D i'm waiting your tutorial for bitmapfont
Posted May 25th 2013 3:54 AM
Kristian Welsh
Kristian Welsh
Very useful little tidbit there, thank you!
Posted July 10th 2013 12:29 PM
Chris
Chris
Thanks for the info. Exactly what I needed.

A tiny improvement of the code:
It's a tad better to write:

private function escOverride(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.ESCAPE) {
e.preventDefault();
}
}

... there is a constant to use for this kind of comparison... it should be used.
Posted October 20th 2013 8:45 AM
Kyle
Kyle
Oh nice! I must've missed the keyboard constants at the time I was using this code ;)
Posted October 21st 2013 3:01 AM
VB
VB
It doesn't work in Flex MenuBar,
with open submenus!
Posted May 5th 2014 10:53 PM
VB
VB
It doesn't work in Flex MenuBar,
with open submenus!

Make menuBar.hight = 0 as it is in real Fullscreen mode and don't bother more about Esc! Then restore later if need.
Posted January 18th 2015 4:07 AM
Daniel Benmergui
Daniel Benmergui
Well this is useful... thanks from the future, Kyle!
Posted July 6th 2015 12:07 PM
Kyle
Kyle
Haha awesome! I don't know if this still works, it's been many many many years since I've used AIR x_x
Posted July 6th 2015 8:07 PM
new comment!

Post your comment!

Name
Email
Comment