为什么我的 KeyPressEvent 不能与右/左/上/下一起使用?

Joe*_*Joe 2 c# keypress

在 C# 中,我试图查看用户是否按下了正确的键,以便玩家向右移动,但是当我尝试时,它没有注册按键:

private void KeyPressed(object sender, KeyPressEventArgs e)
{
      if(e.KeyChar == Convert.ToChar(Keys.Right))
      {
              MessageBox.Show("Right Key");
      } 
} 
Run Code Online (Sandbox Code Playgroud)

它不显示消息框

这也不适用于左/上/下

但是,如果我将其替换为

if(e.KeyChar == Convert.ToChar(Keys.Space))
Run Code Online (Sandbox Code Playgroud)

有用。

难道我做错了什么?

Szy*_*mon 5

您应该使用KeyDown事件,例如箭头:

private void KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

箭头键不是字符,因此KeyPressed不用于它们。