在winforms中检测箭头键

Win*_*der 6 .net c# keypress

可能重复:
向上,向下,向左和向右箭头键不会触发KeyDown事件

首先看代码.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace winform_project
{
 public partial class Form1 : Form
 {

    public Form1()
    {
        InitializeComponent();

    }

    private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
        MessageBox.Show("Hello");
    }
 }
}
Run Code Online (Sandbox Code Playgroud)

我能够检测字母数字键.但是我无法检测箭头键.

在这方面,任何帮助都将受到赞赏.

Win*_*der 27

好吧经过一些研究后我发现处理箭头键事件的最简单方法是重写ProcessCmdKey方法.

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
     if(keyData == Keys.Left)
     {
       MessageBox.Show("You pressed Left arrow key");
       return true;
     }
     return base.ProcessCmdKey(ref msg, keyData);
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.