I need to know how long a keyboard button is held down before it is released. I wrote a class to handle input and record it and everything in my code works just fine for a single keyboard button at a time, but when I choose to hold 2 or more buttons at the same time, only one of the buttons ‘charge time’ is recorded when they are released and the other remains at 0.
public function keyUpHandler(event:KeyboardEvent):void
{
if(event.keyCode == up.num)
KeyReleased(up);
if(event.keyCode == down.num)
KeyReleased(down);
if(event.keyCode == left.num)
KeyReleased(left);
if(event.keyCode == right.num)
KeyReleased(right);
}
public function KeyPressed(key):void
{
if(!key.Down)
{
key.Pressed = true;
key.Down = true;
}
else
{
key.Pressed = false;
key.Down = true;
if(key.charge)
key.chargeTime++;
}
}
public function KeyReleased(key):void
{
key.Down = false;
key.Pressed = false;
//trace("Button: " + key.tag + " Down: " + key.Down + " Pressed: " + key.Pressed + " chargeTime: " + key.chargeTime + " secondsCharged: " + (key.chargeTime/fps));
if(key.charge)
{
key.Released = true;
key.charged = key.chargeTime/fps;
}
}
Is it possible to do this without having to add an event listener on each button?