Detecting a key pressed
syllabus | schedule | assignments | Web Resources | ARTC courses
[Flash Intro]     [Drawing]     [Other Tools]     [Modify/Import]     [Symbols]     [Animation]     [Navigation]     [Publishing]    [Audio]     [ActionScript]     [Textboxes]     [Conditionals/Loops]     [Functions]     [Timing]     [Interaction]     [KeyPressed]    

Detecting a Key Pressed

A simple way to detect if a key is pressed is to make a movie clip and add the following action to it:

Step 1: Make a movie clip
Step 2: Make a Dynamic text box and the Instance Name should be theOutput
Step 3: Add the following action to the Movie Clip Actions (not the frame actions)
on (keyPress "3") {
     _root.theOutput.text = "You pressed 3";
}
Sample file: key1.fla

Using the Arrow Keys to Move an object

Step 1: Make a movie clip
Step 2: Add the following action to the Movie Clip Actions (not the frame actions)
// When you press the Left,Right, Up or Down Arrow key,
//the movie clip to which the handler is attached moves.

on (keyPress "<Left>") {
     this._x -= 10;
}
on (keyPress "<Right>") {
     this._x += 10;
}
on (keyPress "<Up>") {
     this._y -= 10;
}
on (keyPress "<Down>") {
     this._y += 10;
}
Sample file: key2.fla

Figuring out which key is pressed and doing something based on that key

Step 1: Make a movie clip to put the script on it - it can be invisible
Step 2: Make an object to point to the letters and Instance Name should be theObject
Step 3: Make an a lot of letters using Static Text and arrange them across the screen in 2 rows
Step 4: Add the following action to the Movie Clip Actions (not the frame actions)
onClipEvent (keyDown) {
doLetterStuff = function (theLetter) {
switch (theLetter) {
case "a" :
_root.theObject._x = 75;
_root.theObject._rotation = 0;
break;
case "b" :
_root.theObject._x = 175;
_root.theObject._rotation = 0;
break;
case "c" :
_root.theObject._x = 275;
_root.theObject._rotation = 0;
break;
case "d" :
_root.theObject._x = 375;
_root.theObject._rotation = 0;
break;
case "e" :
_root.theObject._x = 475;
_root.theObject._rotation = 0;
break;
case "f" :
_root.theObject._x = 75;
_root.theObject._rotation = 180;
break;
case "g" :
_root.theObject._x = 175;
_root.theObject._rotation = 180;
break;
case "h" :
_root.theObject._x = 275;
_root.theObject._rotation = 180;
break;
case "i" :
_root.theObject._x = 375;
_root.theObject._rotation = 180;
break;
case "j" :
_root.theObject._x = 475;
_root.theObject._rotation = 180;
break;
default :
_root.theObject._x = 0;
_root.theObject._rotation = 90;
break;
}
}

//make a new object to hold the key pressed function
var keyListener = new Object();

keyListener.onKeyDown = function() {
    doLetterStuff(chr(Key.getAscii()));
};

//the Key.addListener registers and object to receive an onKeyDown or onKeyUp notification.
//We send it the variable with the key function in it (defined above).

Key.addListener(keyListener);
    }    //end of the onClipEvent

Sample file: key3.fla

NOTE: The reference to the object must have _root in front of it because the object is at the main level whereas the movieclip is a level inside of the main movie.
Next