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 …
{
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 …
{
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 = "";
}
}
}
Posted in ActionScript 3.0, Bug | 11 Comments »