# Key Combination Detection Problem

**URL:** https://forum.kirupa.com/t/key-combination-detection-problem/275374
**Category:** flash
**Created:** [November 13, 2008, 1:49am UTC](https://forum.kirupa.com/t/key-combination-detection-problem/275374 "2008-11-13T01:49:03Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![skimmmer](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@skimmmer](https://forum.kirupa.com/u/skimmmer)
#### Post date: [November 13, 2008, 1:49am UTC](https://forum.kirupa.com/t/key-combination-detection-problem/275374/1 "2008-11-13T01:49:03Z")

</div>

Hi,

I’m currently making a asteroids-like game and I encountered a problem with detecting key presses.

I’m using the key class made by senocular, which is:

```auto
package {
    
    import flash.display.Stage;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
    
    /**
     * The Key class recreates functionality of
     * Key.isDown of ActionScript 1 and 2. Before using
     * Key.isDown, you first need to initialize the
     * Key class with a reference to the stage using
     * its Key.initialize() method. For key
     * codes use the flash.ui.Keyboard class.
     *
     * Usage:
     * Key.initialize(stage);
     * if (Key.isDown(Keyboard.LEFT)) {
     * // Left key is being pressed
     * }
     */
    public class Key {
        
        private static var initialized:Boolean = false; // marks whether or not the class has been initialized
        private static var keysDown:Object = new Object(); // stores key codes of all keys pressed
        
        /**
         * Initializes the key class creating assigning event
         * handlers to capture necessary key events from the stage
         */
        public static function initialize(stage:Stage):void {
            if (!initialized) {
                // assign listeners for key presses and deactivation of the player
                stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
                stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
                stage.addEventListener(Event.DEACTIVATE, clearKeys);
                
                // mark initialization as true so redundant
                // calls do not reassign the event handlers
                initialized = true;
            }
        }
        
        /**
         * Returns true or false if the key represented by the
         * keyCode passed is being pressed
         */
        public static function isDown(keyCode:uint):Boolean {
            if (!initialized) {
                // throw an error if isDown is used
                // prior to Key class initialization
                throw new Error("Key class has yet been initialized.");
            }
            return Boolean(keyCode in keysDown);
        }
        
        /**
         * Event handler for capturing keys being pressed
         */
        private static function keyPressed(event:KeyboardEvent):void {
            // create a property in keysDown with the name of the keyCode
		
            keysDown[event.keyCode] = true;
        
        
        /**
         * Event handler for capturing keys being released
         */
        private static function keyReleased(event:KeyboardEvent):void {
            if (event.keyCode in keysDown) {
                // delete the property in keysDown if it exists
                delete keysDown[event.keyCode];
            }
        }
        
        /**
         * Event handler for Flash Player deactivation
         */
        private static function clearKeys(event:Event):void {
            // clear all keys in keysDown since the player cannot
            // detect keys being pressed or released when not focused
            keysDown = new Object();
        }
    }
}

```

In my main code I access it by (one example):

```auto
if (Key.isDown(Keyboard.UP))
{
   thrust = .5;
}

```

The problem (that I also had the same problem not using this class as well) is that when a user is pushing down the space bar and the up key, when they push the left key, it doesn’t trigger (but if I try space + up + left it does trigger). I researched a little bit on the web and I think it is something to do with operating systems (I use Win. XP).

Any help would be greatly appreciated! Thanks!
