Mar 17, 2010

Key.isDown(), where did you go?

Got a bit stumped when I realized that multi-keystroke handling was totally removed from AS3. The rabid game programmer in me worked up a little steam while it scurried about the internet looking for a solution. The alternative isn't acceptable, you can't make games without multiple buttons pressed at the same time.

Found a bunch of solutions to this problem, all of them over 100 lines in length. How hard can this problem be?

package Extrude.Utils 
{
 import flash.events.KeyboardEvent; 

 public class Keyhandler
 {
  protected static var _keys:Array = new Array(126);

  public static function onKeyDown(e:KeyboardEvent):void
  { _keys[e.keyCode] = true; }

  public static function onKeyUp(e:KeyboardEvent):void
  { _keys[e.keyCode] = false; }

  public static function isDown(key:uint):Boolean
  { return (_keys[key]); }
 }
}