Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could i recognize 2 separate key presses as one key?

Tags:

c#

So for a game i'm working i have a bunch of different keys doing different things (for example w key makes the player look up and the e key makes the character look up right).

I'm wondering is there a way to make the W + the D key make the player look up right, even though those keys are already being used.

private void FrmGM_KeyDown(object sender, KeyEventArgs e)
    {            
        if (e.KeyCode == Keys.W)
        {
            picplayer.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.IPT_game_player1));
            bulletnumb = 1;
        }

        if (e.KeyCode == Keys.E)
        {
            picplayer.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.IPT_game_player2));
            bulletnumb = 2;
        }

        if (e.KeyCode == Keys.D)
        {
            picplayer.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.IPT_game_player3));
            bulletnumb = 3;
        }

        if (e.KeyCode == Keys.C)
        {
            picplayer.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.IPT_game_player4));
            bulletnumb = 4;
        }

        if (e.KeyCode == Keys.X)
        {
            picplayer.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.IPT_game_player5));
            bulletnumb = 5;
        }

        if (e.KeyCode == Keys.Z)
        {
            picplayer.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.IPT_game_player6));
            bulletnumb = 6;
        }

        if (e.KeyCode == Keys.A)
        {
            picplayer.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.IPT_game_player7));
            bulletnumb = 7;
        }

        if (e.KeyCode == Keys.Q)
        {
            picplayer.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.IPT_game_player8));
            bulletnumb = 8;
        }

this is the code for my keys presses

like image 203
Fidelio Avatar asked Aug 23 '15 22:08

Fidelio


People also ask

How do you use multiple keys on a keyboard?

Most standard keyboards allow users to press three control keys at once and an additional key. These keys are usually on both the right and left sides of your keyboard. There are usually two Ctrl buttons, two Shift buttons, and two Alt buttons.

How many keys can be pressed at once on a keyboard?

Most keyboards cannot handle more than 6 inputs at once. But also sometimes certain key combinations just don't work. This concept is called "rollover" Which really just means, how well can your keyboard understand that multiple keys are being pressed at once?

How is keyPressed function used in processing?

Click on the window to give it focus and press the letter keys to type colors. The keyboard function keyPressed() is called whenever a key is pressed. keyReleased() is another keyboard function that is called when a key is released.

Why can I only press 3 keys on keyboard?

Because you have a cheap keyboard. It cuts corners by transmitting several key signals over the same bus, which means they can ghost one another. If you want to be able to press any number of keys at once, you'll need a keyboard with n-key rollover (nkro), which is a property of gaming keyboards.


2 Answers

Store letters in a set when the KeyDown events arrive, and remove them in the KeyUp event handler. This way your KeyDown would be able to "see" if the "companion key" has been pressed:

private readonly ISet<char> keysCurrentlyDown = new HashSet<char>();

private void FrmGM_KeyDown(object sender, KeyEventArgs e) {            
    if (e.KeyCode == Keys.W)
    {
        if (keysCurrentlyDown.Contains('D')) {
            // Call some method to handle W+D
        } else {
            ...
        }
        keysCurrentlyDown.Add('W');
    } else if (e.KeyCode == Keys.D)
    {
        if (keysCurrentlyDown.Contains('W')) {
            // Call some method to handle W+D
        } else {
            ...
        }
        keysCurrentlyDown.Add('D');
    }
    ...
}
private void FrmGM_KeyUp(object sender, KeyEventArgs e) {            
    if (e.KeyCode == Keys.W) {
        keysCurrentlyDown.Remove('W');
    } else if (e.KeyCode == Keys.D) {
        keysCurrentlyDown.Remove('D');
    } ...
}

}
like image 156
Sergey Kalinichenko Avatar answered Sep 19 '22 14:09

Sergey Kalinichenko


The first line of offense for this sort of thing is just to make sure that each key can be activated concurrently, and that the combination of the effects of each key have the result you want. Your code will be more maintainable and easier to write if you follow that approach, rather than trying to special-case the combine key input.

For example, you can have a set of flags that represent the current key state, and then on your game's frame update, check those flags and set the character state to the correct thing (i.e. instead of setting specific images in the key-handling itself, as you're doing now).

So you could have flags like:

bool lookUp, lookRight, lookDown, lookLeft;

Then key-down for W would set the lookUp flag, while key-up for the same key would clear the flag. Likewise e.g. for D, for the lookRight flag.

Then on your game frame update, you check the flags. If lookUp and lookRight are both set, then you use the "look up and right" graphic. If just one or the other flag are set, then you use the plain "look up" or "look right" graphic. Likewise for other keys and their combinations. You could even resolve lookUp and lookDown, or lookLeft and lookRight, as whatever the default graphic would be (i.e. as if the user pressed no keys).


Note that if you're willing to use p/invoke, you can use the GetAsyncKeyState() function, and not even bother with the key-down event. Just check the key state before each frame update for the game, and update the visuals according to the current keyboard state. That way, you let Windows do all the key state tracking, and you don't waste time tracking key state that doesn't matter (e.g. if the user causes multiple changes to the key state between frames).


That said, if you can't/won't do any of the above, then yes...all you need to do is check for the combination first (i.e. you got the key-down for both, without a key-up for the initially-pressed key of the combination).

Do note that taking that approach means that you will still take the effect for the first key pressed, and then have to switch to the combination effect.

like image 29
Peter Duniho Avatar answered Sep 16 '22 14:09

Peter Duniho