Saturday, July 18, 2009

Catch Keyboard Event (Key Down) with ActionScript

// catch Key Down Event

stage.addEventListener( KeyboardEvent.KEY_DOWN, KeyDownEventHandler);


function KeyDownEventHandler(e:KeyboardEvent ){
    trace(e.charCode);
    trace(e.keyCode);


    if( e.keyCode == Keyboard.UP){
        // use keyCode to detect special key

    }
    else if( e.keyCode == Keyboard.DOWN){
    }
    else if( e.keyCode == Keyboard.LEFT){
    }
    else if( e.keyCode == Keyboard.RIGHT){
    }
    else if( e.keyCode == Keyboard.SPACE){
    }
    else if( e.keyCode == Keyboard.SHIFT){
    }
    else if( e.charCode == 98){
        // b, ascii code = 98

        // use charCode to detect character
    }
    else if( e.charCode == 108){
        // l
    }
    else if( e.charCode == 97){
        // a
    }
    else if( e.charCode == 65){
        // A
    }
    else if( e.charCode == 118){
        // v
    }
    else if( e.charCode == 114){
        // r
    }
    else{
        //
    }
}

No comments: