试图检测按键

我做了一个方法来检测按键何时被按下,但它不起作用! inheritance我的代码

void KeyDetect(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.W && firstload == true) { MessageBox.Show("Good, now move to that box over to your left"); firstload = false; } } 

我也尝试创建一个keyeventhandler但是,它“无法分配给密钥检测,因为它是一个方法组”

 public Gwindow() { this.KeyDetect += new KeyEventHandler(KeyDetect); InitializeComponent(); } 

使用这样的按键事件:

 private void Form1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyCode == Keys.F1 && e.Alt) { //do something } } 

你正在寻找this.KeyPress 。 请参阅如何在MSDN上处理Keypress事件 。

尝试使用KeyDown事件。

只需在MSDN中查看KeyDown