Skip to main content
Mar 28, 2014 4:46 AM UTC
Answered

Help on AS2 keyListener script

Hi, I have been this ActionScript a lot -

var keyListener:Object = new Object();

Key.addListener(keyListener);

function keyDownF(){

    play();

}

keyListener.onKeyDown = keyDownF;

But would like to know how to revise it so I could isolate the specific keys I do or don't want to be used.

Also is it possible to disable so it only works at a particular part in my time line? If I use it now it will work on every frame.

Any advice would be helpful.

Thanks again

Jason

This topic is closed to new replies. Start a new post to keep the conversation going.
Ned Murphy
Ned MurphyCorrect answer
Legend
Mar 28, 2014 11:38 AM UTC

Hi Jason, here is a simplified bit of code that works based on specific keys.  You would just replace the "trace()" commands with your intended actions...

var keyListener:Object = new Object();
Key.addListener(keyListener);

keyListener.onKeyDown = function(){


    // trace(Key.getCode()); // use this trace to determine other key codes


    if(Key.getCode() == 38) trace("up");
    if(Key.getCode() == 40) trace("down");
    if(Key.getCode() == 37) trace("left");
    if(Key.getCode() == 39) trace("right");
}

As far as only allowing this to function in specific frames, you could utilize other conditionals that check if the _currentframe property is a value that allows for it.  You could also assign the Key.onKeyDown to be null when you do not want it to be active.

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
Mar 28, 2014 11:38 AM UTC

Hi Jason, here is a simplified bit of code that works based on specific keys.  You would just replace the "trace()" commands with your intended actions...

var keyListener:Object = new Object();
Key.addListener(keyListener);

keyListener.onKeyDown = function(){


    // trace(Key.getCode()); // use this trace to determine other key codes


    if(Key.getCode() == 38) trace("up");
    if(Key.getCode() == 40) trace("down");
    if(Key.getCode() == 37) trace("left");
    if(Key.getCode() == 39) trace("right");
}

As far as only allowing this to function in specific frames, you could utilize other conditionals that check if the _currentframe property is a value that allows for it.  You could also assign the Key.onKeyDown to be null when you do not want it to be active.

Translate