Check keyboard status without using KeyboardEvent in AS3

Can I test keystrokes without using KeyboardEvent?

I have an ENTER_FRAME event setting called enterFrameHandler, and I want to check inside the enterFrameHandler function if any keys are pressed.

usually when using KeyboardEvent I can easily check keys using a switch that checks KeyCode events, but in the ENTER_FRAME event this is not possible for me.

Is there any other way to check the status of the keyboard in 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 is similar to what I want, but in AS2, does anyone know how to “translate” this into AS3?

+3
3

:

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;
}
+5

- KeyboardEvent?

0

When listening KeyboardEvent.KEY_DOWNto the scene, keeping the key pressed is very simple, so there is no need for 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;
    }

}
0
source

Source: https://habr.com/ru/post/1740567/


All Articles