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