KeyboardManagement for Games in Actionscript 3

A solution to multiple keys being held down at once. When you listen to KEY_UP in flex, or actionscript 3.0 there are some issues by acting on the KEY_UP Event. Especially for games in actionscript 3.0.

Application Source

In the Application, start off by pressing “D” to move right, and “A” to move left. The problem is that when you are holding D to move right, lets say you press “W” to move up (now i don’t have W set up to work in this application, but none the less) the whole point is to have “D” still executing while you lift up back off from “W”.

To accomplish this, I created a keyboardManager Singleton class, the class holds an array of keys that are down (downKeys:Array). and 2 functions that execute on keyUp and keyDown.

On keyDown it takes a look at the downKeys array and see’s if it is empty… if so, then it adds the keyEvent to the array, and then it exits…

On keyUp it loops over the downKeys Array and splices it removing the correct keyEvent out of the array….

There is one trick that I do though… there is an outer document that actually adds the listeners for the keyUp and keyDown, in those listener functions, thats where I call the keyboardManager. The trick is on the keyUp listener, I do some checking to find out if I need to re-call the keyDown functions for the keys that are still down…

Typically in the type of game that I am making, I’m only going to be using W,S,D,A for my keys then adding some mouse interaction later on. I wanted the feel with the keyboard to be better than some of the examples I’ve seen, where the KeyUp Event is where things just get removed or speed gets set back to zero…

I wanted to be able to press “D”, then press “A” to back up a little, and take my finger off of “A” with the original “D” still pressed, and have “D” still be getting called…

Anyway it’s late, and I’m tired (it’s 2:45 am)…


ps… 360flex kicked ass… Thanks to the crew of guitar hero’ers that made it feel real homey… and for the huge attempts at getting rockband working…. a big thanks to Tom, and John for putting on the conference… it was by far the best conference i’ve been to.

Application Source

10 comments on this post.
  1. Managing bounds in Actionscript 3.0 | Axelology:

    […] is a continuation off from my post Keyboard Management for games in Actionscript 3 where I talk about managing multiple keys being held down at once, and how to continue executing the […]

  2. nate:

    Yeah I ran into the same thing when I needed to write a keyboard shortcut manager to be able to handle multiple key combos.

    shift + control + p would trigger the action, and not trigger the same action as control + p

    Only think is I created a keyboard shortcut manager that would check to see if the current keys down matched specifically a set of keys that were registered with an action.

    It also needed to check for shift + control + p
    in addition to control + shift + p, etc; depending on how the user pressed the keys and how the key was entered into the shortcut manager.

  3. stroiteldoma:

  4. Foto, video, digital:

  5. Game Dev Job:

  6. sizeaspem:

    How to Backup PlayStation2 Games on cd???
    Pls, help me!

  7. master66:

  8. Ken:

    KeyboardManagement for Games in Actionscript 3

    When key_down any key,and don’t key_up,and click outside of flahplayer.You will find the object still moving. If flashplayer lose focus.Can we key_up all key?

  9. aw:

    However, i found a bug:

    When I press W(up) and D(right), the ship goes up-right, that’s okay.

    But when I release D, it should be ahead the up, but it is still going ahead up-right.

    I think this should be regarded as a bug.

    Anyway, thanks for your great work! Hope you can fix it 🙂

  10. aw:

    anyway, the KeyboardManagement itself is really great!!

    But the code in your main class seems need to change.

    I put it to the enterFrame loop and use a “if…else” to judge how many keys are “down” to get if solved. However, maybe there’s better solution 🙂

Leave a comment