Return to Kroll Design home page

Flash Tutorials (with .FLA files included)

 
« Return

ActionScript 3

KeyboardEvent: responding to multiple simultaneous key presses

In this example, you can change the walking cartoon character's dimensions using each of the 4 arrow keys. You can also press two keys at once. Pressing the up and down or left and right keys simultaneously will cancel each other out, but if you press the up and left keys at the same time, for example, the Flash movie responds to both keys at once.

Download the FLA file



// define constants:
const leftArrow:uint = 37;
const upArrow:uint = 38;
const rightArrow:uint = 39;
const downArrow:uint = 40;
const speed:Number = .03;
const originalSize:Number = .5

// define text field containing instructions:
var instructions:TextField = new TextField();
instructions.wordWrap = true;
instructions.width = 450;
instructions.x = 100;
instructions.text = "Use the arrow keys to change the size of the man. Try all 4 keys, and try pressing more than one key at once.  You'll soon see what works and what doesn't.  Click the reset button to start over."

// define boolean variables to indicate if each arrow key is being pressed:
var leftArrowPressed:Boolean = false;
var upArrowPressed:Boolean = false;
var rightArrowPressed:Boolean = false;
var downArrowPressed:Boolean = false;

// place an animated walking man on the stage:
var walker_mc:Walker = new Walker();
walker_mc.x = 250;
walker_mc.y = 250;
walker_mc.scaleX = originalSize;
walker_mc.scaleY = originalSize;
addChild(walker_mc);

// place a Start button on the stage. Clicking this is needed to provide the
// Flash movie with keyboard focus:
var startButton_btn:StartButton = new StartButton();
startButton_btn.x = 50;
startButton_btn.y = 20;
startButton_btn.scaleX = .7;
startButton_btn.scaleY = .7;
addChild(startButton_btn);

// set up Reset button. Clicking this restores the man to 
// his original size.  The Reset button will be placed on the stage
// when the Start button is clicked.
var resetButton_btn:ResetButton = new ResetButton();
resetButton_btn.x = 50;
resetButton_btn.y = 20;
resetButton_btn.scaleX = .7;
resetButton_btn.scaleY = .7;

// define listeners for arrow keys, the Enter Frame event,
// the Start button and the Reset button:
stage.addEventListener(Event.ENTER_FRAME, resizeWalker);
startButton_btn.addEventListener(MouseEvent.CLICK, clickStartButton);
resetButton_btn.addEventListener(MouseEvent.CLICK, resetSize);

// see if arrow keys were pressed or released:
function processKeys(myEvent:KeyboardEvent):void
{
	if (myEvent.type == "keyDown")
	{
		if (myEvent.keyCode == leftArrow)
		{	leftArrowPressed = true; }
		
		else if (myEvent.keyCode == upArrow)
		{	upArrowPressed = true; }
		
		else if (myEvent.keyCode == rightArrow)
		{	rightArrowPressed = true; }
		
		else if (myEvent.keyCode == downArrow)
		{	downArrowPressed = true; }
	}
	
	
	if (myEvent.type == "keyUp")
	{
		if (myEvent.keyCode == leftArrow)
		{	leftArrowPressed = false; }
		
		else if (myEvent.keyCode == upArrow)
		{	upArrowPressed = false; }
		
		else if (myEvent.keyCode == rightArrow)
		{	rightArrowPressed = false; }
		
		else if (myEvent.keyCode == downArrow)
		{	downArrowPressed = false; }
	}	
}

// if arrow keys are being pressed, resize walking man accordingly:
function resizeWalker(myEvent:Event):void
{
	if (leftArrowPressed)
	{ walker_mc.scaleX -= speed; }
	
	if (upArrowPressed)
	{ walker_mc.scaleY += speed; }
	
	if (rightArrowPressed)
	{ walker_mc.scaleX += speed; }
	
	if (downArrowPressed)
	{ walker_mc.scaleY -= speed; }	
}

// When the Start button is clicked, do the following:
function clickStartButton(myEvent:MouseEvent):void
{
	stage.addEventListener(KeyboardEvent.KEY_DOWN, processKeys);
  stage.addEventListener(KeyboardEvent.KEY_UP, processKeys);
	startButton_btn.visible = false;
	addChild(resetButton_btn);
	addChild(instructions);
}

// When the Reset button is clicked, restore the man's original size:
function resetSize(myEvent:MouseEvent):void
{
	walker_mc.scaleX = originalSize;
	walker_mc.scaleY = originalSize;	
}


 
« Return


©2012 Kroll Design    info@KrollDesign.net    781.910.3694
Last modified: 12/31/1969 7:00 PM