Flash Player dispatches KEY_DOWN event when entering FullScreen mode (Bug)

September 17th, 2009 by Adrian

Have just built a video player that allowed the user to switch to FullScreen mode and toggle the play/pause state using the SPACE key on their keyboard. However, I found that everytime the Flash movie entered FullScreen mode the playing video would pause and visa-versa.

I then discovered that the Flash Player was dispatching a KEY_DOWN event when entering FullScreen mode. More specifically, the keyCode == 32, which corresponds to the spacebar on your keyboard.

My Flash Player version is WIN 10,0,32,18

See an example of it here …

Here is code for the above demo movie …

package
{
    import flash.display.MovieClip;
    import flash.ui.Keyboard;
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.display.StageDisplayState;
   
    import fl.controls.Button;

    public class FullscreenSpaceBug extends MovieClip
    {
       
        public var tf:TextField;
        public var clearBtn:Button;
        public var goBtn:Button;
        public var exitBtn:Button;

        public function FullscreenSpaceBug():void
        {
            trace("FullscreenSpaceBug()");
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onStage_KEY_DOWN);
            clearBtn.addEventListener(MouseEvent.CLICK, onClearBtn_CLICK);
            clearBtn.focusEnabled = false;
            clearBtn.useHandCursor = true;
            goBtn.addEventListener(MouseEvent.CLICK, onGoBtn_CLICK);
            goBtn.useHandCursor = true;
            exitBtn.addEventListener(MouseEvent.CLICK, onExitBtn_CLICK);
            exitBtn.useHandCursor = true;  
            tf.text = "";
        }
       
        private function onGoBtn_CLICK(event:MouseEvent):void
        {
            stage.displayState = StageDisplayState.FULL_SCREEN;
        }
       
        private function onExitBtn_CLICK(event:MouseEvent):void
        {
            stage.displayState = StageDisplayState.NORMAL;
        }
       
        private function onStage_KEY_DOWN(event:KeyboardEvent):void
        {
            if (event.keyCode == 32) {
                tf.appendText("event.keyCode = "+event.keyCode+"   (SPACE)\n");
            } else {
                tf.appendText("event.keyCode = "+event.keyCode+"\n");
            }
            tf.scrollV = tf.maxScrollV;
        }
       
        private function onClearBtn_CLICK(event:MouseEvent):void
        {
            tf.text = "";
        }

    }

}

I managed to find an entry for this bug in the Adobe Flash Player Bug and Issue Management System (JIRA Database). FP-814: Flash Player dispatches KEY_DOWN event when entering FullScreen Mode

I also found a very similar blog post by TyZ which was posted on 25th August 2009 titled ‘Flash bug when enter fullscreen keyboard events fired – Workaround‘.

The workaround is to temporarily remove the KEY_DOWN event listener, and add it again with a slight delay once the FULL_SCREEN event has been dispatched. You could create the delay by using either a Timer, an ENTER_FRAME event or the setTimeout method. In TyZ’s example he used an ENTER_FRAME event, in my example below I use an instance of the Timer class.

WORKAROUND EXAMPLE

Here is the code for the workaround …

package
{
    import flash.display.MovieClip;
    import flash.display.StageDisplayState;
    import flash.ui.Keyboard;
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;
    import flash.events.FullScreenEvent;
    import flash.text.TextField;
    import flash.events.TimerEvent;
    import flash.utils.Timer;

   
    import fl.controls.Button;

    public class FullscreenSpaceWorkaround extends MovieClip
    {
       
        public var tf:TextField;
        public var clearBtn:Button;
        public var goBtn:Button;
        public var exitBtn:Button;

        public function FullscreenSpaceWorkaround():void
        {
            trace("FullscreenSpaceBug()");
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onStage_KEY_DOWN);
            stage.addEventListener(FullScreenEvent.FULL_SCREEN, onStage_FULL_SCREEN);
            clearBtn.addEventListener(MouseEvent.CLICK, onClearBtn_CLICK);
            clearBtn.focusEnabled = false;
            clearBtn.useHandCursor = true;
            goBtn.addEventListener(MouseEvent.CLICK, onGoBtn_CLICK);
            goBtn.useHandCursor = true;
            exitBtn.addEventListener(MouseEvent.CLICK, onExitBtn_CLICK);
            exitBtn.useHandCursor = true;  
            tf.text = "";
        }
       
        private function onGoBtn_CLICK(event:MouseEvent):void
        {
            stage.removeEventListener(KeyboardEvent.KEY_DOWN, onStage_KEY_DOWN);
            stage.displayState = StageDisplayState.FULL_SCREEN;
        }
       
        private function onExitBtn_CLICK(event:MouseEvent):void
        {
            stage.displayState = StageDisplayState.NORMAL;
        }
       
        private function onStage_FULL_SCREEN(event:FullScreenEvent):void
        {
            if (event.fullScreen) {
                var timer:Timer = new Timer(200, 1);
                timer.addEventListener(TimerEvent.TIMER, onTimer);
                timer.start();

            }
        }
       
        private function onTimer(event:TimerEvent):void
        {
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onStage_KEY_DOWN);
        }
       
        private function onStage_KEY_DOWN(event:KeyboardEvent):void
        {
            if (event.keyCode == 32) {
                tf.appendText("event.keyCode = "+event.keyCode+"   (SPACE)\n");
            } else {
                tf.appendText("event.keyCode = "+event.keyCode+"\n");
            }
            tf.scrollV = tf.maxScrollV;
        }
       
        private function onClearBtn_CLICK(event:MouseEvent):void
        {
            tf.text = "";
        }

    }

}

Download the source files for the workaround here

Posted in ActionScript 3.0, Bug | 11 Comments »

AS3 KeyPoll class

April 15th, 2008 by Adrian

Richard Lord over at Flash Game Code has released a few useful AS3 classes. One of which is the KeyPoll class. This is really useful because the Key.isDown event no longer exists in ActionScript 3.

Here is a quick example I knocked up …

The Flash source code for this can be downloaded here.

Posted in ActionScript 3.0, Flash | 2 Comments »

Detecting key presses in AS3

February 25th, 2008 by Adrian

Keyboard

In ActionScript 3 you can detect key presses on the keyboard using the Stage class. I discovered that the Stage object is not globally accessible. You need to access it through the stage property of a DisplayObject instance. So from within a Class you can check which keys are being pressed using the following code.

package {
    import flash.events.KeyboardEvent;
    import flash.display.MovieClip;

    public class KeyboardTest extends MovieClip {

        private var base:MovieClip;

        public function KeyboardTest($base:MovieClip):void {
            trace("KeyboardTest constructor is being run");
            base=$base;
            base.stage.addEventListener(KeyboardEvent.KEY_DOWN,keyboardListener);
        }

        private function keyboardListener(event:KeyboardEvent):void {
            trace("event.keyCode: " + event.keyCode);
        }
    }
}

Posted in ActionScript 3.0, Sample Code | No Comments »