Tip: Flash Key Codes

It seems like whenever I need this I can never find it so I’m posting it here for my own future reference as well as for everyone else to have. These are the key codes that are returned by the KeyboardEvent in Flash so that you don’t have to run the actual method to retrieve them.

If you found this post useful, please consider leaving a comment, subscribing to the feed, or making a small donation.

23 Comments

It is a bit annoying that the Keyboard class does not have constants for each letter (at least). I use a simple custom class for that that goes a something like this:

public class KeyCode {
public static const A:uint = 65;
public static const B:uint = 66;
public static const C:uint = 67;
[…]
public static const X:uint = 88;
public static const Y:uint = 89;
public static const Z:uint = 90;
}

I have a code snippet for this… you an just use a for loop to create all your variables:

// create constants for all letter and number keys:
var alphabet:Array = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”.split(“”);
var nums:Array = [“ZERO”,”ONE”,”TWO”,”THREE”,”FOUR”,”FIVE”,”SIX”,”SEVEN”,”EIGHT”,”NINE”];

var key:Object = new Object();
for (var i:int = 0; i<alphabet.length; i++)
key[alphabet[i]] = 65 + i;

for (i = 0; i<nums.length; i++){
var code:int = 48 + i;
key[nums[i]]= code;
key[i] = code;
}

Then you can just use key.A, key.B, key.NINE or key[“9”] and an if statement:

if (evt.keyCode == key.A){
trace(“a was pressed”);
}

bartek, thats a great idea, i think i’ll do that.

Zevan, that’s pretty awesome as well, altho I do like the idea of having a class with all the constants in it as bartek posted.

The constants are definitely best practice… 🙂

Looks like in AS3 theres a top level Keyboard class that holds all the constants you’d need…
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/ui/Keyboard.html

JP,
While those constants do exist, they are only available in the AIR runtime as pointed out by the icon next to each one. Why this is is absolutely beyond me as they should be available to use in Flash as well, but I do have a remedy for it that I will be posting soon based on Bartek’s KeyCode class. I’m just trying to figure out if I want to jam a couple of more things into the class before it is posted or post as is so I’ll keep up with that and post it in the new year.

@JP the letter keys at least seem to be AIR only….

Well that stumps me too why they would only be available in AIR…glad to here you’ll be posting a useful class for that!

Has anybody made a utility class that stores all the keys as constants yet (for flash, as the Keyboard class only has them for AIR)?

I’m willing to contribute if someone is working on one and needs extra typing fingers.

@JP

no need for typing skills…. you can simply do something like this:
var alphabet:Array = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”.split(“”);
var nums:Array = [“ZERO”,”ONE”,”TWO”,”THREE”,”FOUR”,”FIVE”,”SIX”,”SEVEN”,”EIGHT”,”NINE”];
for (var i:int = 0; i<alphabet.length; i++)
trace(“public static const “+ alphabet[i] + “:int = ” + (65 + i) + “\n”);

for (i = 0; i<nums.length; i++){
trace(“public static const “+ nums[i] + “:int = ” +(48 + i) + “\n”);
}

use ActionScript to write the class for you…

I actually did make one that I now use just didn’t post it yet. I was going to wait until I got some time to write some methods in there for using multiple keys at once and all that so its all wrapped in one class, but if you want it now I can post what I have so far and then just version it out later.

I’ll let you finish 😉
If I get need it desperately before you post the final one I’ll let you know…
Thanks

cool, i’ll try to get it done soon, i’ve just had small projects pop up out of the blue here and there and they all seem to have to be done in like two days time 😛

@Zevan,

Ah, using ActionScript to write ActionScript, I like your style 😉

Hello:

var nums:Array = [“ZERO”,”ONE”,”TWO”,”THREE”,”FOUR”,”FIVE”,”SIX”,”SEVEN”,”EIGHT”,”NINE”];

Could I ask if evt.keyCode is inside key:Object = new Object();

or the number of elements inside key

Thanks for all and sorry my bad english

@israel: i’m not exactly sure i follow what you’re trying to ask. key is just a generic object so evt.keyCode would not at that point be a part of it. i think you’re looking at the AS2 version of this in which case after you add key as a listener to the Key class in AS2 it would then produce the event object. not sure if that makes any sense or not but hopefully this is what you were after.

Thanks for answer I resolve with textfield attributes

_tabla._Omin.maxChars=2
_tabla._Oseg.restrict = “0-9”;

With this limit the char that you put in the text field.

Thanks for all.

Hey Zevan,

thanks for posting that. Very clever short hand solution. I decided to just throw it into a class for anyone else who comes along in the future.

Thanks again,
dela

public class KeyCodes
{
public const INVALID_INPUT:String = “null”;

private var alphabet:Array;
private var nums:Array;

private var key:Object;

public function KeyCodes()
{
init();
}

private function init():void
{
// create constants for all letter and number keys:
alphabet = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”.split(”);
nums = [“ZERO”,”ONE”,”TWO”,”THREE”,”FOUR”,”FIVE”,”SIX”,”SEVEN”,”EIGHT”,”NINE”];

key = new Object();
for (var i:int = 0; i < alphabet.length; i++) {
key[ alphabet[i] ] = 65 + i;
}

for (i = 0; i < nums.length; i++){
var code:int = 48 + i;
key[ nums[i] ] = code;
}
}

public function translateCodeIntoKey( keyCode:uint ):String
{
for ( var keyName:String in key ) {
if ( keyCode == key[keyName] ) {
return keyName;
}
}
return INVALID_INPUT;
}
}

i need code plz send me

Hey guys, I’m having trouble coding this little flash game I’m trying to build… I was using CS5 on my uni computers and coded it fine there using keycodes such as

stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_PressKeyToMove_2);

function fl_PressKeyToMove_2(event:KeyboardEvent):void {
switch (event.keyCode) {

case Keyboard.F:;

{
movieClip_2.blasterfire_mc.gotoAndPlay(“combo”);
break;
trace(event.keyCode);

}
And soon realised CS4 would not allow this… So I managed to change the code to get the commands to work using the SPACE,SHIFT & ENTER key codes but can’t get it to work using just letters like F,D & C like I did with CS5… I would prefer those 3 keys as I would like them to relate to the command in the game such as “Fire, Dash & Combo”. I’m Only new to coding and all of the previous posts I have found on this page confuse the c**p out of me! So please help. I can deal with using the SHIFT & SPACE keys but enter seems a little spazzo.

Thanx Raz

hi guys im just new in adobe flash cs4 and i want to know if how to make sample program at actionscript 3.0 that everytime i press key down the movie clip would be remove. i was making a typing lesson and im still cant figure it out on how to make it, can u help me guys?please

Leave a comment

(required)

(required)