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.
Now here's what the escOverride function looks like.
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.)
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);
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.)
10 Comments