0

I'm making a game in C# in Forms in Visual Studio and I'm currently registering key presses from the player with the keyDown event and it works well. When the player is holding down a button it gets pressed once, there is a delay and then the key is registered many times over and over, which is good, but if any other key that I'm checking for gets pressed while the first key is being held, the new key is registered once and then nothing more, just like when typing. I want it so that if key is pressed while another is being held, the first key is registered once, and then the first key, that's being held, keeps outputting.

I'm currently getting:

aaaaaaaaaaaaaaaaaap

but I want:

aaaaaaaaaaaaaaaaaapaaaaaaaaaaaaaa

if that makes sense

Is it possible?

Here is my current method:

private void GUItetris_KeyDown(object sender, KeyEventArgs e)
{
    if (!Tetris.gameOver && Tetris.gameBoard != null)
    {
        if (e.KeyCode == Keys.A) // a
        {
            Tetris.gameBoard.activePiece.MoveLeft(Tetris.gameBoard.boardColor);
            Tetris.gameBoard.UpdatePiece();
        }

        if (e.KeyCode == Keys.D) //d
        {
            Tetris.gameBoard.activePiece.MoveRight(Tetris.gameBoard.boardColor);
            Tetris.gameBoard.UpdatePiece();
        }
        if (e.KeyCode == Keys.S) //s
        {
            Tetris.gameBoard.activePiece.MoveDown(Tetris.gameBoard.boardColor);
            Tetris.gameBoard.UpdatePiece();
        }
        if (e.KeyCode == Keys.P) // p
        {
            Tetris.gameBoard.activePiece.Rotate(Tetris.gameBoard.boardColor, true);
            Tetris.gameBoard.UpdatePiece();
        }
        if (e.KeyCode == Keys.L) // l
        {
            Tetris.gameBoard.activePiece.Rotate(Tetris.gameBoard.boardColor, false);
            Tetris.gameBoard.UpdatePiece();
        }
    }
}
GSerg
  • 76,472
  • 17
  • 159
  • 346
Roblol
  • 1
  • Instead of reacting to key presses, poll the keys in a timer tick handler using [Keyboard.IsKeyDown(Key)](https://learn.microsoft.com/en-us/dotnet/api/system.windows.input.keyboard.iskeydown). You probably want the key checks happening somewhat more often than the block moves. – Andrew Morton Dec 13 '20 at 17:46
  • Does this answer your question? [How to detect key down/up when a second key is already held down](https://stackoverflow.com/questions/11314401/how-to-detect-key-down-up-when-a-second-key-is-already-held-down) – GSerg Dec 13 '20 at 17:49

0 Answers0