Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法捕获Windows窗体中向上/向下箭头键的向下键事件_C#_Winforms_Keydown_Keyup_Arrow Keys - Fatal编程技术网

C# 无法捕获Windows窗体中向上/向下箭头键的向下键事件

C# 无法捕获Windows窗体中向上/向下箭头键的向下键事件,c#,winforms,keydown,keyup,arrow-keys,C#,Winforms,Keydown,Keyup,Arrow Keys,在C#Winforms中,我尝试捕获向上箭头键和向下箭头键的KeyDown事件。因此,我做了以下工作: 将窗体的KeyPreview属性设置为true 表单“OnKeyDown”方法的重写 无论如何,该方法永远不会为向上/向下键调用,尽管它是为左/右箭头键调用的。然后我也尝试覆盖表单“OnKeyUp”方法,只是为了测试。奇怪的是,现在对上/下箭头也调用了“OnKeyUp”方法。我还尝试重写“ProcessCmdKey”,结果是相同的:向上/向下箭头不调用它 我不能使用KeyUp事件的原因是,我需

在C#Winforms中,我尝试捕获向上箭头键和向下箭头键的KeyDown事件。因此,我做了以下工作:

  • 将窗体的KeyPreview属性设置为true
  • 表单“OnKeyDown”方法的重写
  • 无论如何,该方法永远不会为向上/向下键调用,尽管它是为左/右箭头键调用的。然后我也尝试覆盖表单“OnKeyUp”方法,只是为了测试。奇怪的是,现在对上/下箭头也调用了“OnKeyUp”方法。我还尝试重写“ProcessCmdKey”,结果是相同的:向上/向下箭头不调用它

    我不能使用KeyUp事件的原因是,我需要意识到如果按键保持按下一段时间,那么我需要多次调用该事件,而对于KeyUp则不是这样


    关于这里可能出现的问题有什么建议吗?

    我的项目中似乎有一个自定义控件,它已经覆盖了ProcessCmdKey方法。这个超控吞没了我的箭头键。直到现在我才知道这个覆盖。

    在自定义控件基类中检查那里有哪些可用的事件。然后重写desire事件或使用reflector查看自定义控件Dll的内部代码

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