Search code examples
actionscript-3flashair

AS3 - preventDefault() doesn't work in fullscreen (AIR)


I am trying to make a fullscreen program in air that does not become windowed when you press escape. Why is my program not working as it should, and how should make it work correct?

Code:

package 
{
import flash.display.*;
import flash.events.*;
import flash.desktop.*;
import flash.text.*;

public class Main extends Sprite 
{

    public function Main():void 
    {
        stage.scaleMode = StageScaleMode.NO_SCALE;
        stage.align = StageAlign.TOP_LEFT;
        stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandeler);

    }

    private function keyDownHandeler(e:KeyboardEvent):void
    {
        if (e.keyCode == 27) 
        {
            trace("Hello");
            e.preventDefault();
        }
    }
}

}


Solution

  • I created a new project (FlashDevelop) targeting AIR 14 (also successfully tried AIR 3.9) with the following document class file:

    package 
    {
        import flash.desktop.NativeApplication;
        import flash.display.NativeWindow;
        import flash.display.NativeWindowInitOptions;
        import flash.display.NativeWindowRenderMode;
        import flash.display.NativeWindowSystemChrome;
        import flash.display.Sprite;
        import flash.display.StageDisplayState;
        import flash.events.Event;
        import flash.events.FullScreenEvent;
        import flash.events.KeyboardEvent;
        import flash.events.MouseEvent;
        import flash.ui.Keyboard;
    
        public class Main extends Sprite 
        {
    
            public function Main():void 
            {
                if (stage) init();
                else addEventListener(Event.ADDED_TO_STAGE, init);
            }
    
            private function init(e:Event = null):void 
            {
                if(e) removeEventListener(Event.ADDED_TO_STAGE, init);
    
                stage.addEventListener(KeyboardEvent.KEY_DOWN, keyUpHandler);
    
                var content:Sprite = new Sprite();
                content.graphics.beginFill(0xFFFF00);
                content.graphics.drawRect(0, 0, 400, 500);
                content.graphics.endFill();
                addChild(content);
    
                this.addEventListener(MouseEvent.CLICK, fullScreen);
    
                stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
            }
    
            private function fullScreen(e:Event):void {
                stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
            }
    
            protected function keyUpHandler(event:KeyboardEvent):void {
                switch(event.keyCode) {
                    case Keyboard.ESCAPE:
                        trace("ESCAPE");
                        event.preventDefault();
                        break;
    
                    default:
                        trace("UNHANDLED KEY: ", event.keyCode);
                }
            }
    
    
        }
    
    }
    

    It worked as expected. When hitting escape, the preventDefault() method on the key event successfully kept the application in full-screen.

    Notes:
    It has to be on the key down. Key up had no effect. The result was the same with the key down listener on the stage or the nativeApplication.