Keyboard combinations

Hi everyone,

I’m working on a project that involves using different combinations of the LEFT DOWN and RIGHT keys to interact with a trumpet on screen. I’m having trouble getting flash to recognize different key combinations though. I found this code in the forums:

var aKeysPressed:Array = new Array();

stage.addEventListener( KeyboardEvent.KEY_DOWN, handleKeyDown );
stage.addEventListener( KeyboardEvent.KEY_UP, keyUp );

function handleKeyDown( event:KeyboardEvent ):void {
aKeysPressed.push( event.keyCode );
}

function keyUp(event:KeyboardEvent):void {
// 37 = LEFT, 38 = UP, 39 = RIGHT
if ( aKeysPressed.length == 2 ) {

    if ( aKeysPressed[ 0 ] == 37 || aKeysPressed[ 1 ] == 37 ) {
        if ( aKeysPressed[ 0 ] == 38 || aKeysPressed[ 1 ] == 38) {
            trace( " LEFT & UP pressed!" );
        }
        
    } else if ( aKeysPressed[ 0 ] == 38 || aKeysPressed[ 1 ] == 38 ) {
        if ( aKeysPressed[ 0 ] == 39 || aKeysPressed[ 1 ] == 39 ) {

            trace( " UP & RIGHT pressed!" );
        }
    }
    
}
aKeysPressed = [];

}

but it doesn’t recognize the combinations unless you press the combination all together. I want something to recognize when a combination is pressed, even if one of the keys was already down. I’ve found some other methods, but I feel as though they are a little over my head. Any help would be greatly appreciated.

Thank you!