00:00
00:00

As3 Customize Controls Key Mapping

894 Views | 24 Replies
New Topic Respond to this Topic

Hey everybody! I wanted to post this here since I have been using it recently whenever I make a game that lets the user customize their key inputs/controls. It's basically a couple functions that let you convert as3 key codes into key names and vice versa. I got tired of visiting this page over and over: http://www.dakmm.com/?p=272

There is probably something already built into as3 for this, but I haven't seen it... ENJOY!

var kmap = new Array();
kmap.push(new Array(8,"Backspace"));
kmap.push(new Array(9,"Tab"));
kmap.push(new Array(13,"Enter"));
kmap.push(new Array(16,"Shift"));
kmap.push(new Array(17,"Control"));
kmap.push(new Array(20,"Capslock"));
kmap.push(new Array(27,"Esc"));
kmap.push(new Array(32,"Spacebar"));
kmap.push(new Array(33,"PageUp"));
kmap.push(new Array(34,"PageDown"));
kmap.push(new Array(35,"End"));
kmap.push(new Array(36,"Home"));
kmap.push(new Array(37,"LeftArrow"));
kmap.push(new Array(38,"UpArrow"));
kmap.push(new Array(39,"RightArrow"));
kmap.push(new Array(40,"DownArrow"));
kmap.push(new Array(45,"Insert"));
kmap.push(new Array(46,"Delete"));
kmap.push(new Array(144,"Numlock"));
kmap.push(new Array(145,"ScrLk"));
kmap.push(new Array(19,"Pause/Break"));
kmap.push(new Array(65,"A"));
kmap.push(new Array(66,"B"));
kmap.push(new Array(67,"C"));
kmap.push(new Array(68,"D"));
kmap.push(new Array(69,"E"));
kmap.push(new Array(70,"F"));
kmap.push(new Array(71,"G"));
kmap.push(new Array(72,"H"));
kmap.push(new Array(73,"I"));
kmap.push(new Array(74,"J"));
kmap.push(new Array(75,"K"));
kmap.push(new Array(76,"L"));
kmap.push(new Array(77,"M"));
kmap.push(new Array(78,"N"));
kmap.push(new Array(79,"O"));
kmap.push(new Array(80,"P"));
kmap.push(new Array(81,"Q"));
kmap.push(new Array(82,"R"));
kmap.push(new Array(83,"S"));
kmap.push(new Array(84,"T"));
kmap.push(new Array(85,"U"));
kmap.push(new Array(86,"V"));
kmap.push(new Array(87,"W"));
kmap.push(new Array(88,"X"));
kmap.push(new Array(89,"Y"));
kmap.push(new Array(90,"Z"));
kmap.push(new Array(65,"a"));
kmap.push(new Array(66,"b"));
kmap.push(new Array(67,"c"));
kmap.push(new Array(68,"d"));
kmap.push(new Array(69,"e"));
kmap.push(new Array(70,"f"));
kmap.push(new Array(71,"g"));
kmap.push(new Array(72,"h"));
kmap.push(new Array(73,"i"));
kmap.push(new Array(74,"j"));
kmap.push(new Array(75,"k"));
kmap.push(new Array(76,"l"));
kmap.push(new Array(77,"m"));
kmap.push(new Array(78,"n"));
kmap.push(new Array(79,"o"));
kmap.push(new Array(80,"p"));
kmap.push(new Array(81,"q"));
kmap.push(new Array(82,"r"));
kmap.push(new Array(83,"s"));
kmap.push(new Array(84,"t"));
kmap.push(new Array(85,"u"));
kmap.push(new Array(86,"v"));
kmap.push(new Array(87,"w"));
kmap.push(new Array(88,"x"));
kmap.push(new Array(89,"y"));
kmap.push(new Array(90,"z"));
kmap.push(new Array(48,"0"));
kmap.push(new Array(49,"1"));
kmap.push(new Array(50,"2"));
kmap.push(new Array(51,"3"));
kmap.push(new Array(52,"4"));
kmap.push(new Array(53,"5"));
kmap.push(new Array(54,"6"));
kmap.push(new Array(55,"7"));
kmap.push(new Array(56,"8"));
kmap.push(new Array(57,"9"));
kmap.push(new Array(186,";:"));
kmap.push(new Array(187,"=+"));
kmap.push(new Array(189,"-_"));
kmap.push(new Array(191,"/?"));
kmap.push(new Array(192,"`~"));
kmap.push(new Array(219,"[{"));
kmap.push(new Array(220,"\|"));
kmap.push(new Array(221,"]}"));
kmap.push(new Array(222,"\'\""));
kmap.push(new Array(188,","));
kmap.push(new Array(190,"."));
kmap.push(new Array(191,"/"));
kmap.push(new Array(96,"Numpad 0"));
kmap.push(new Array(97,"Numpad 1"));
kmap.push(new Array(98,"Numpad 2"));
kmap.push(new Array(99,"Numpad 3"));
kmap.push(new Array(100,"Numpad 4"));
kmap.push(new Array(101,"Numpad 5"));
kmap.push(new Array(102,"Numpad 6"));
kmap.push(new Array(103,"Numpad 7"));
kmap.push(new Array(104,"Numpad 8"));
kmap.push(new Array(105,"Numpad 9"));
kmap.push(new Array(106,"Numpad Multiply"));
kmap.push(new Array(107,"Numpad Add"));
kmap.push(new Array(13,"Numpad Enter"));
kmap.push(new Array(109,"Numpad Subtract"));
kmap.push(new Array(110,"Numpad Decimal"));
kmap.push(new Array(111,"Numpad Divide"));
kmap.push(new Array(112,"F1"));
kmap.push(new Array(113,"F2"));
kmap.push(new Array(114,"F3"));
kmap.push(new Array(115,"F4"));
kmap.push(new Array(116,"F5"));
kmap.push(new Array(117,"F6"));
kmap.push(new Array(118,"F7"));
kmap.push(new Array(119,"F8"));
kmap.push(new Array(120,"F9"));
kmap.push(new Array(122,"F11"));
kmap.push(new Array(123,"F12"));
kmap.push(new Array(124,"F13"));
kmap.push(new Array(125,"F14"));
kmap.push(new Array(126,"F15"));

function codeToWord(code)
{
	var word = "[unknown: "+code+"]";
	for (var i=0;i<kmap.length;i++)
	{
		if (code==kmap[i][0])
		{
			word=kmap[i][1];
		}
	}
	return(word);
}

function wordToCode(word)
{
	var code = "[unknown: "+word+"]";
	for (var i=0;i<kmap.length;i++)
	{
		if (word==kmap[i][1])
		{
			code=kmap[i][0];
		}
	}
	return(code);
}

//example of using the codeToWord function
var nam = codeToWord(80);
trace(nam);
//outputs: "p"

//example of using the wordToCode function
var code = codeToWord("p");
trace(code);
//outputs: 80

Response to As3 Customize Controls Key Mapping 2014-01-14 15:00:51


Sorry, my 2nd example should be using wordToCode instead of codeToWord... typo!

Response to As3 Customize Controls Key Mapping 2014-01-14 15:06:26


This is something that I had to do when I was 15 years old.
I made key mapping in one of my games.
This is useful.
You might want to wrap it into a class and maybe host the class file on github.
ActionScript 2 neither 3 have this.
Now just need to make sure that children with Flash who are looking for this may actually find it.

Response to As3 Customize Controls Key Mapping 2014-01-14 15:11:00


At 1/14/14 03:06 PM, kkots wrote: This is something that I had to do when I was 15 years old.
I made key mapping in one of my games.
This is useful.
You might want to wrap it into a class and maybe host the class file on github.
ActionScript 2 neither 3 have this.
Now just need to make sure that children with Flash who are looking for this may actually find it.

Awww, child's play! :-P

But good to know it's useful. Feel free to wrap it in a class and post it.

I prefer finding tutorials with the raw in-line code cuz it's easier to turn something into a class than it is to try to extract from a class back to in-line code. Easier for noobz to understand too.

Response to As3 Customize Controls Key Mapping 2014-01-14 15:48:10


At 1/14/14 03:11 PM, BoMToons wrote: to try to extract from a class back to in-line code

Why would one want to do that?

I'd try to do this with code instead
Converting names to keycodes is easy with the Keyboard class.
To invert the map, one can use describeType().

Response to As3 Customize Controls Key Mapping 2014-01-14 16:03:29


It would have been a lot easier to use a dictionary instead of an array. You're also not breaking out of the for loop after the code/word has been found.

Hell, you could have just used two dictionaries and wouldn't need any for loop at all:

var keyCode:int = codesDict["A"];
var keyName:String = namesDict[98];

Response to As3 Customize Controls Key Mapping 2014-01-14 16:07:46


At 1/14/14 03:48 PM, milchreis wrote:
At 1/14/14 03:11 PM, BoMToons wrote: to try to extract from a class back to in-line code
Why would one want to do that?

Cuz they're unfamiliar with how to use classes perhaps...

Response to As3 Customize Controls Key Mapping 2014-01-14 16:09:19


At 1/14/14 04:03 PM, Diki wrote: It would have been a lot easier to use a dictionary instead of an array. You're also not breaking out of the for loop after the code/word has been found.

Hell, you could have just used two dictionaries and wouldn't need any for loop at all:

var keyCode:int = codesDict["A"];
var keyName:String = namesDict[98];

I'm not familiar with using dictionaries...

True, you should return the found item right away to be more efficient! Good catch playa!

Response to As3 Customize Controls Key Mapping 2014-01-14 16:16:47


At 1/14/14 04:03 PM, Diki wrote: Hell, you could have just used two dictionaries and wouldn't need any for loop at all:

var keyCode:int = codesDict["A"];
var keyName:String = namesDict[98];

Show us a full example of how to do it using dictionaries plz :-D

Response to As3 Customize Controls Key Mapping 2014-01-14 16:19:41


At 1/14/14 04:09 PM, BoMToons wrote: I'm not familiar with using dictionaries...

You should read up on them; dictionaries are very useful. They're used for storing key/value pairs where the key can be whatever you want, such as a string or an integer. So you could store the key codes and names like this:

var codesDict:Dictionary = new Dictionary;

codesDict["A"] = 65;
codesDict["B"] = 66;
codesDict["C"] = 67;
var namesDict:Dictionary = new Dictionary;

namesDict[65] = "A";
namesDict[66] = "B";
namesDict[67] = "C";

And access them like in the example code in my previous post. No for loop required.

Response to As3 Customize Controls Key Mapping 2014-01-14 16:21:13


At 1/14/14 04:19 PM, Diki wrote: var codesDict:Dictionary = new Dictionary;

codesDict["A"] = 65;
codesDict["B"] = 66;
codesDict["C"] = 67;

or just use the Keyboard class as mentioned above.

Response to As3 Customize Controls Key Mapping 2014-01-14 16:23:08


At 1/14/14 04:21 PM, milchreis wrote: or just use the Keyboard class as mentioned above.

Oh yeah, derp, I meant to comment on that. You should do what milchreis suggested, I was just showing you how you could make your code simpler if you were to use dictionaries.

Response to As3 Customize Controls Key Mapping 2014-01-14 16:25:38


At 1/14/14 04:23 PM, Diki wrote:
At 1/14/14 04:21 PM, milchreis wrote: or just use the Keyboard class as mentioned above.
Oh yeah, derp, I meant to comment on that. You should do what milchreis suggested, I was just showing you how you could make your code simpler if you were to use dictionaries.

Dictionaries look interesting, are they faster than for looping through the array though? (anyone benchmarked this?)

Cuz your method requires writing 2 maps (doubling the lines of code) but simplifying the call to one line. If it's faster, then it's worth it.

@milchreis: let's see an example of what you're describing plz.

Response to As3 Customize Controls Key Mapping 2014-01-14 16:30:57


At 1/14/14 04:25 PM, BoMToons wrote: Dictionaries look interesting, are they faster than for looping through the array though? (anyone benchmarked this?)

Cuz your method requires writing 2 maps (doubling the lines of code) but simplifying the call to one line. If it's faster, then it's worth it.

They're technically slower, but I can guarantee you're not doing anything that will need that level of performance. We're talking a difference of like five milliseconds.

At 1/14/14 04:25 PM, BoMToons wrote: @milchreis: let's see an example of what you're describing plz.

He's talking about this.

Response to As3 Customize Controls Key Mapping 2014-01-14 16:36:34


(Posting from alt)

They're technically slower, but I can guarantee you're not doing anything that will need that level of performance. We're talking a difference of like five milliseconds.

So slower and double the code... :-P

At 1/14/14 04:25 PM, BoMToons wrote: @milchreis: let's see an example of what you're describing plz.
He's talking about this.

I know, but I want to see how it'd work practically.

Response to As3 Customize Controls Key Mapping 2014-01-14 17:09:34


At 1/14/14 04:36 PM, PestoForce wrote: So slower and double the code... :-P

Dictionaries are my shit. There's a point when you're iterating through so many things that dictionary access is faster, not really sure where that is, though.

At 1/14/14 04:25 PM, BoMToons wrote: @milchreis: let's see an example of what you're describing plz.
He's talking about this.
I know, but I want to see how it'd work practically.

dict[Keyboard.A] = "A";

Keyboard.whatever replaces the need for you to write the number.

Response to As3 Customize Controls Key Mapping 2014-01-14 17:09:58


At 1/14/14 04:25 PM, BoMToons wrote: At 1/14/14 04:36 PM, PestoForce wrote: I know, but I want to see how it'd work practically.
trace(Keyboard.A);
// which is the same as
trace(Keyboard["A"]);
// so
trace(Keyboard[name]);

Response to As3 Customize Controls Key Mapping 2014-01-14 17:52:00


At 1/14/14 05:09 PM, milchreis wrote:
At 1/14/14 04:25 PM, BoMToons wrote: At 1/14/14 04:36 PM, PestoForce wrote: I know, but I want to see how it'd work practically.
trace(Keyboard.A);
// which is the same as
trace(Keyboard["A"]);
// so
trace(Keyboard[name]);

So the user presses a key on their keyboard, take us from there to you telling them the name of the one they pressed...

Response to As3 Customize Controls Key Mapping 2014-01-14 18:05:13


At 1/14/14 05:52 PM, PestoForce wrote: So the user presses a key on their keyboard, take us from there to you telling them the name of the one they pressed...

As said, build the inverse map with describeType()

Response to As3 Customize Controls Key Mapping 2014-01-14 18:35:37


At 1/14/14 06:05 PM, milchreis wrote:
At 1/14/14 05:52 PM, PestoForce wrote: So the user presses a key on their keyboard, take us from there to you telling them the name of the one they pressed...
As said, build the inverse map with describeType()

You hate typing code I guess. I don't know how describeType works and I do best with examples, so I'm curious how you'd do it syntactically.

Response to As3 Customize Controls Key Mapping 2014-01-14 19:27:11


At 1/14/14 06:35 PM, BoMToons wrote: You hate typing code I guess.

Especially if it's a bunch of c&p lines that seem to be hard to maintain and tedious to put down.

I don't know how describeType works

It describes the type of something in As3.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/package.html#describeType%28%29

something like this:

package{
public class KeycodeNameMap
{
    private static const _keyNameMap:Object = {};
    
    public static function init():void
    {
        var name:String;        
        for each (var constant:XML in describeType(Keyboard).constant.(@type == "uint"))
            _keyNameMap[Keyboard[name = constant.@name]] = name;
    }

    public static function nameToKeycode(name:String):uint
    {
        return Keyboard[name] ? Keyboard[name] : 0;
    }
    
    public static function keycodeToName(keycode:uint):String
    {
        return _keyNameMap[keycode] ? _keyNameMap[keycode] : "";
    }
}
}

Using an Object as a map, could be a Dictionary as well or even an Array as the key codes are uints.

There's Keyboard.CharCodeStrings, which looked promising, but it seems like it doesn't really contain all the characters.

Response to As3 Customize Controls Key Mapping 2014-01-14 20:28:07


At 1/14/14 07:27 PM, milchreis wrote: something like this:

You are the winner! I tested it and it works! Definitely the niftiest solution and maintains itself.

Here's a test .swf to prove it:
http://www.newgrounds.com/dump/item/9db15013684d6c22005bc9f3335935c8

Here's a .fla (cs5.5) if anyone ever wants to grab it (though your code above works perfectly):
http://www.newgrounds.com/dump/item/d7d2f4bbb27686e35675e9bad8205cff

The only benefit to my way, I guess, is that you could give the keys unique names like "Da Eff Key" but who would ever need to do that? Ha ha!

Response to As3 Customize Controls Key Mapping 2014-01-14 20:33:38


At 1/14/14 08:28 PM, BoMToons wrote:
At 1/14/14 07:27 PM, milchreis wrote: something like this:

I guess it also might be a bummer to have it say "SEMICOLON" instead of the symbol ";" for some applications.

Response to As3 Customize Controls Key Mapping 2014-01-14 20:39:23


At 1/14/14 08:33 PM, BoMToons wrote: I guess it also might be a bummer to have it say "SEMICOLON" instead of the symbol ";" for some applications.

Yes, think different languages.

Response to As3 Customize Controls Key Mapping 2014-01-14 21:31:43


At 1/14/14 07:27 PM, milchreis wrote: something like this:

Really nice little class, good job.