3

Is it possible to check for pressed keys without using the KeyboardEvent?

I have an ENTER_FRAME event setup called enterFrameHandler and I want to check within the function enterFrameHandler if any keys are pressed.

normally when using a KeyboardEvent I could check for keys easily using a switch that checks the KeyCode of the event, but in an ENTER_FRAME event this isn't possible for me.

Is there any other way of checking the keyboard's state within the ENTER_FRAME event?

UPDATE: I found this AS2 script:

onClipEvent (enterFrame) {
    if (Key.isDown(Key.LEFT)) {
        _x -= power;
    }
    if (Key.isDown(Key.RIGHT)) {
        _x += power;
    }
    if (Key.isDown(Key.UP)) {
        _y -=power;
    }
    if (Key.isDown(Key.DOWN)) {
        _y +=power;
    }
}

This seems to be doing what I want, but it's in AS2, does anyone know how to 'translate' this into AS3?

Pieter888
  • 4,882
  • 13
  • 53
  • 74
  • See my answer for a related question here: http://stackoverflow.com/questions/2501424/what-is-the-most-effective-way-to-test-for-combined-keyboard-arrow-direction-in-a/2502380#2502380 – Cameron Apr 10 '10 at 16:03

3 Answers3

5

Store key states in a dictionary or object:

stage.addEventListener(KeyboardEvent.KEY_UP, keyHandleUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandleDown);

private var hash:Object = {};

private function keyHandleUp(event:KeyboardEvent):void {
    delete hash[event.keyCode];
}

private function keyHandleDown(event:KeyboardEvent):void {
    hash[event.keyCode] = 1;
}

private function isKeyDown(code:int):Boolean {
    return hash[code] !== undefined;
}
ansiart
  • 2,563
  • 2
  • 23
  • 41
  • The above code does not compile because event.keycode is missing the capital C. "keyCode" – Craig Dec 27 '14 at 13:40
0

The short answer is no Why don't you want to use a KeyboardEvent event listener?

just_a_dude
  • 2,745
  • 2
  • 26
  • 30
  • I could, but I'm curious now I know this is possible with AS2 – Pieter888 Apr 10 '10 at 12:02
  • 1
    @just_a_dude. Being able to check whether a particular key is down at a given time is very convenient. For example, in the main loop of a game, where you check for user input at a centralized point. That is what this class lets you do: http://code.google.com/p/bigroom/source/browse/trunk/src/uk/co/bigroom/input/KeyPoll.as. It was possible to do it natively in AS 2. Not sure why now you are forced to implement it in Actionscript, but I digress. – Juan Pablo Califano Apr 11 '10 at 16:08
  • @JuanPabloCalifano you don't need to digress Juan. keep gressing. gress all you want. also, thanks for the link but it is giving a 404. do you have a example of what it was doing? – 1.21 gigawatts Jul 31 '15 at 08:33
0

With a KeyboardEvent.KEY_DOWN listener added to the stage, keeping the key pressed works just fine, so there's no need for the ENTER_FRAME.

private function keyDownHandler(evt:KeyboardEvent):void
{
    switch(evt.keyCode)
    {
        case 37: //left key
                trace("Move left");
            break;
        case 38: //up key
            trace("Move up");
            break;
        case 39: //right key
            trace("Move right");
            break;
        case 40: //down key
            trace("Move down");
            break;
    }

}
Jorge Guberte
  • 10,464
  • 8
  • 37
  • 56
  • Or you could use an EventDispatcher on ENTER_FRAME, but i'm pretty sure it'd run way slower than using the KeyboardEvent. – Jorge Guberte Apr 10 '10 at 12:47