Flash AS3 - CharCodeStrings Library (download)
A few weeks ago I was working on an interesting text typing widget for our new website (that isn’t up yet, still in the works). I came to find that there was a Keyboard.CharCodeStrings Array Constant in the AS3 Language reference–but it was only available within the AIR 1.0 runtime. Bummer.
So like many folks out there, I needed to roll a homegrown solution to match up CharCode result numbers with their character counterparts. Essentially a singleton class that you can use when you catch your event and turn the resulting CharCode into a literal string character.
Here is an example:
-
package {
-
import flash.events.KeyboardEvent;
-
import flash.display.MovieClip;
-
import com.iq.CharCodeStrings;
-
public class CharCodeStrings_sample extends MovieClip {
-
-
public function CharCodeStrings_sample() {
-
stage.addEventListener(KeyboardEvent.KEY_DOWN, traceKey);
-
}
-
public function traceKey(event:KeyboardEvent):void {
-
var myChar:String = CharCodeStrings.getChar(event.charCode);
-
trace(myChar);
-
}
-
}
-
}
If this may be useful to you, there is a download link below. I built this to do what I needed for the project I was working on, so the character codes returned are for US English–but you could easily substitute another language by using the included sample app and replacing the characters. If you do set this up for use in another language feel free to shoot me the file and I will link it here.
(Hint, when testing from within the Flash IDE turn off Keyboard Shortcuts when you run your movie to test or the preview window may not receive keyboard input. It is in the Control menu when your swf is playing.)





[...] but then found out that it is only available
Away3D Typewriter : timovirtanen.com[...] but then found out that it is only available within the AIR 1.0 runtime. Fortunately I found Scott Ruttencutters CharCodeStrings Library (download). Thanks for sharing it Scott [...]
Looks like your class only handles printable ASCII characters (no
hwuagLooks like your class only handles printable ASCII characters (no “page up”, “left arrow”, etc). In that case, why not just use
String.fromCharCode()?You, sir, Win the game. Why adobe would hide
adminYou, sir, Win the game. Why adobe would hide a function in the string Class is beyond me. Thanks for pointing it out.