views:

619

answers:

3

I'm developing a little game. I use the following code to detect the keys pressed by the player:

private function onKeyDown(event:KeyboardEvent):void {
        //moviment keys
        if (event.keyCode == 37 || event.keyCode == 65) {
            this.leftKeyPressed = true;
        }
        if (event.keyCode == 39 || event.keyCode == 68) {
            this.rightKeyPressed = true;
        }
        if (event.keyCode == 38 || event.keyCode == 87) {
            this.upKeyPressed = true;
        }
        if (event.keyCode == 40 || event.keyCode == 83) {
            this.downKeyPressed = true;
        }

        if (event.keyCode == this.shootKey) {
            this.shootKeyPressed = true;
        }
    }

The onKeyUp event:

private function onKeyUp(event:KeyboardEvent):void {
        if (event.keyCode == 37 || event.keyCode == 65) {
            this.leftKeyPressed = false;
        }
        if (event.keyCode == 39 || event.keyCode == 68) {
            this.rightKeyPressed = false;
        }
        if (event.keyCode == 38 || event.keyCode == 87) {
            this.upKeyPressed = false;
        }
        if (event.keyCode == 40 || event.keyCode == 83) {
            this.downKeyPressed = false;
        }
        if (event.keyCode == this.shootKey) {
            this.shootKeyPressed = false;
        }
        if (event.keyCode == changeColorKey) {
            trace('color key released');
            trace(getTimer());
            this.changeColorKeyPressed = true;
        }

    }

Basically the shootKey will be hold down by the player almost all the time. And the changeColorKey will be pressed very often but not hold down. While testing I noted that holding the shootKey and the right arrow down, the onKeyUp events for the changeColorKey don't get fired. Holding the up or down arrow key instead of the right arrow has the same effect. If I hold the left arrow key the events get fired. Why does it occour? Is there something wrong with my code?

+1  A: 

I would recommend using the Keypoll library, you can find it here: http://code.google.com/p/bigroom/wiki/KeyPoll.

As for how many keys you can hold down, I'm not sure, but try using ctrl, shift as usually does not count towards the limit (I have not tested this in Flash/ActionScript).

Lillemanden
I tried the KeyPoll class and it showed the same problem with the key presses.
Ricardo
Ok, properly a Flash limitation then. Did you try and use ctrl and shift for some of the functionality?
Lillemanden
+2  A: 

The problem you are seeing is not related to your code, but to your keyboard. Depending on the keyboard technology the number and combinations of keys you can press simultaneously varies. I did a code very similar to yours a couple of years ago and I remember having the same problem you mention, while with my current keyboard that no longer happens (it seems this can support up to five simultaneous letters while the old one supported three).

LopSae
And a couple of articles about this issue (and even how to hack a keyboard).http://www.extremetech.com/article2/0,2845,2301151,00.asphttp://www.extremetech.com/article2/0,2845,1656177,00.aspIt seems usually usb keyboards supports up to six simultaneous key, so if you have an older keyboard this may be a good moment to upgrade it.
LopSae
+2  A: 

I believe the problem has to do with the hardware keyboard also. I have tested the code myself on my keyboard, and I can't press more then 3 keys at the same time on a non-usb keyboard.

Now in your example, that should not be a problem, because you never need to press more then 3 keys. But watch out! There is another limitation that seems to be in the hardware keyboard and it has to do with the proximity of the keys you are pressing with each other.

For example, when trying your code, it seemed I could not receive a KEY_UP when I had two other keys KEYed_DOWN(meaning pressed down continually) next to the one I'm KEYing_UP(so the one I want to receive the key_up event for). So I believe that is why your code only works with the left arrow key. Your up, down and right arrow keys are probably, somehow, too close to the changeColorKey and because of this, (I'm guessing because of how the circuitry is done in a keyboard) it can't trigger it's up state.

Try moving the shootKey and/or the changeColorKey somewhere else, farther away and see if that works.

didibus