Capture key sequences in ActionScript 3. AKA The Konami Comeback.
by Martin on 6/06/2009
Lately the internet has had a wave of sites using the cherished Konami Cheatcode to reveal easter eggs or similar functionality in the site or app.
I wrote a (crappy but working) class to implement such functionality in ActionScript about a year ago here.
I decided to polish it up a bit for the benefit of myself and anyone else and in the process ended up creating a small utility class to work with keystrokes in the process.
You can download the (fairly self-expaining) classes and a sample project right here, or read on for a bit of explanations.
(Text) Keys.as.
(Text) Konami.as.
(Archive) Neatly zipped + example.
Keys.as
Working with keyCodes in ActionScript can be a real drag. Capturing KeyboardEvents and mapping keyCodes to characters and that whole dreary mess…
So I decided to setup a class with constants for the most commonly used keyCodes on western keyboards. Curiously the native Keyboard class will do this for AIR projects, but not for Flash Player, so here goes. In addition I put a method in there to get an array of keyCodes from a string.
Usage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import com.ctrloptcmd.string.Keys; trace(Keys.A, Keys.B, Keys.UP, Keys.NINE); /* Returns: 65, 66, 38, 57 */ trace(Keys.getKeyCodes("Cheat Code")); /* Returns: 67,72,69,65,84,32,67,79,68,69 */ stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp); function onKeyUp( ke : KeyboardEvent) : void { if(ke.keyCode == Keys.A){ trace("Key A"); } } |
As I said. Pretty straightforward. I’ll be adding to this class whenever I feel changes are in order but for now it does one thing well.
Konami.as
This class, named after the aforementioned and hallowed Konami Code, lets you listen for a sequence of characters entered in the exact order without mistakes.
It’ll dispatch an event (Konami.CODE_COMPLETE) when the code has successfully been entered which you can react upon as you wish. It expects a reference to stage and an array of keyCodes (hint: Keys.getKeyCodes("Your Magic String")). The last is optional and the class defaults to the Konami Code.
There is a loose dependency on the Keys class, but if you really want to use this class without the Keys class it’s a pretty easy job to cut the strings (no pun intended) as described in the comments in the class itself.
Usage:
1 2 3 4 5 6 7 8 |
Possible future functionality:
- Konami.as: Time restrictions. Right now you can start entering the code, take a break and finish it half an hour later. Works fine for my purposes, but I might add an optional time parameter later.
- Konami.as: Failure event. It might be interesting to get a notification when the code sequence was interrupted. It might…
- Keys.as: I don’t know how this holds up against different international keyboard layouts. This was authored on a Norwegian keyboard, and seems to work fine with US layouts apart from the extra special chars. I’d like to do some research on this and see if any measures could be implemented to generalize the class.
Those files again:
(Text) Keys.as.
(Text) Konami.as.
(Archive) Neatly zipped + example.
That’s all folks.
There are 3 comments in this article: