# \[AS3\] Problem 'Charging' Two Buttons and Releasing

**URL:** <https://forum.kirupa.com/t/as3-problem-charging-two-buttons-and-releasing/328470>\
**Category:** flash\
**Created:** [May 19, 2012, 3:20pm UTC](https://forum.kirupa.com/t/as3-problem-charging-two-buttons-and-releasing/328470 "2012-05-19T15:20:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Scheisse](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@Scheisse](https://forum.kirupa.com/u/Scheisse)\
**Post date:** [May 19, 2012, 3:20pm UTC](https://forum.kirupa.com/t/as3-problem-charging-two-buttons-and-releasing/328470/1 "2012-05-19T15:20:09Z")

</div>

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.

```auto
        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?
