0
votes

In C# Winforms I try to capture the KeyDown event for the Up-arrow and down-arrow keys. Therefore I did the following:

  1. set the KeyPreview property of the form to true
  2. override of the forms 'OnKeyDown' method

Anyway, the method is never called for the up-/ down-keys, although it is called for the left-/ right-arrow-keys for example. Then I tried to override the forms 'OnKeyUp' method too, just for testing. The strange thing is now that the 'OnKeyUp' method is called for the up-/ down-arrows too. I also tried to override 'ProcessCmdKey', with the same result: it is not called for the up-/ down-arrows.

The thing why I can't use the KeyUp event is that I need to realize if the keys keep pressed for a time so I need the event to get called multiple times which isn't the case for KeyUp.

Any suggestions on what could be the problem here?

3

3 Answers

0
votes
private bool downKey = false, rightKey = false, leftKey = false;

private void TetrisGame_KeyDown(object sender, KeyEventArgs e)
{
      Graphics g = Graphics.FromImage(canvas);
      if (e.KeyCode == Keys.Down && CurrentBlock.canMoveDown())
      {
            downKey = true;
            CurrentBlock.moveDown(g);
            if (rightKey && CurrentBlock.canMoveRight()) CurrentBlock.moveRight(g);
            else if (leftKey && CurrentBlock.canMoveLeft()) CurrentBlock.moveLeft(g);
      }
      else if (e.KeyCode== Keys.Down)
      {
            downKey = true;
            newBlock();
      }
      else if (e.KeyCode == Keys.Right && CurrentBlock.canMoveRight())
      {
            rightKey = true;
            CurrentBlock.moveRight(g);
            if (downKey && CurrentBlock.canMoveDown()) CurrentBlock.moveDown(g);
                else if (downKey) newBlock();
      }
      else if (e.KeyCode == Keys.Right)
      {
            rightKey = true;
            if (downKey && CurrentBlock.canMoveDown()) CurrentBlock.moveDown(g);
            else if (downKey) newBlock();
      }
      else if (e.KeyCode == Keys.Left && CurrentBlock.canMoveLeft())
      {
            leftKey = true;
            CurrentBlock.moveLeft(g);
            if (downKey && CurrentBlock.canMoveDown()) CurrentBlock.moveDown(g);
            else if (downKey) newBlock();
      }
      else if (e.KeyCode == Keys.Left)
      {
            leftKey = true;
            if (downKey && CurrentBlock.canMoveDown()) CurrentBlock.moveDown(g);
            else if (downKey) newBlock();
      }
      this.Refresh();
}

private void TetrisGame_KeyUp(object sender, KeyEventArgs e)
{
      if (e.KeyCode == Keys.Down)
            downKey = false;
      else if (e.KeyCode == Keys.Right)
            rightKey = false;
      else if (e.KeyCode == Keys.Left)
            leftKey = false;
}
0
votes

It seems that there is a custom control in my project which already overrides the ProcessCmdKey method. This override swallowed up my arrow-key-press. I didn't know about this override until now.

0
votes

Check in the Custom Control base class what are the events are available there. Then override the desire event or Use reflector to see internal code of the Custom Control Dll.