Detect key press in Actionscript 3
I have previously created a tutorial called Keyboard controls in AS3 where you learn how to move an object around the stage using the keyboard arrow keys. In this tutorial you will learn how to detect the other keyboard keys such as the letters, spacebar, enter key, shift key etc. You will also learn how to detect multiple key presses on the keyboard. Every character on the keyboard has a unique key code number which has been mapped specifically to a location on the keyboard. For example, the letter ‘a’ is the key code number 65.
Detect key press in Actionscript 3
Step 1
Open a new Flash AS3 file.
On the timeline select the first frame then open up the Actions panel and enter the following code.
//Adds event listener to the stage.
stage.addEventListener(KeyboardEvent.KEY_DOWN, detectKey);
//Traces the keycode, keylocation, shiftkey and altkey.
function detectKey(event:KeyboardEvent):void {
trace("The keypress code is: " + event.keyCode);
trace("The keyLocation is: " + event.keyLocation);
trace("The shiftKey is: " + event.shiftKey);
trace("The altKey is: " + event.altKey);
}
Step 2
Test your move clip Ctrl + Enter. Now if you don’t see any numbers being traced out, you need to disable the keyboard shortcuts. On the SWF menu bar click on Control > Disable Keyboard Shortcuts and then you should see the key code numbers being traced out. You will notice that the uppercase and lowercase characters produce the same key code.

You can also trace a message when certain characters on the keyboard have been pressed. In the example below only the ‘enter key’ and the ‘j’ key will display a message, the other key will display nothing.
if (event.keyCode==13) {
trace("The enter key has been pressed");
}
if (event.keyCode==74) {
trace("The j has been pressed");
}
The following example will display a message when the alt, control and shift key have been pressed.
if (event.shiftKey == true) {
trace("The Shift key has been pressed");
}
if (event.altKey== true) {
trace("The Alt key has been pressed");
}
if (event.ctrlKey == true) {
trace("The Ctrl key has been pressed");
}
You can also detect multiple key presses. This example will trace a message when the shift and ‘a’ keys have been pressed.
if (event.shiftKey== true && event.keyCode==65 ) {
trace("The Shift key and a key has been pressed");
}



8 comments:
Hi, thanks for your post. But it giving errors below:
'{' expected
Unexpected '}' encountered
Hope you can help!
@Dan
Make sure you have not left out any '{' or '}'.
Hi, If i would like to code "spacebar" should be pressed only once. what will be the code look like? Thanks for your help in advance.
figured it out.. thanks!
Great, but I couldn't get certain keyboard combinations to work together. Any letter plus any number doesn't work (at least not on the version of cs3 I'm using).
@Nicholas
Which keys don't work?
Hey is there a way to have certain keys do one thing on one page and then different on another, as i use
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDownn);
function myKeyDownn(e:KeyboardEvent):void
{
if (e.keyCode == Keyboard.SPACE)
{
gotoAndPlay("welcome");
}
}
But then on next frame i cant use space bar again to do another task?
Any help would be appreciated
@Shant
If you are coding on the main timeline then you can’t access code from another frame. You would have to place the contents into movie clip.
Post a Comment