2012-10-19 47 views
2

我做了一个方法来检测按键何时按下,但它不工作!我的继承人代码试图检测按键

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

回答

5

这样使用按键事件:

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

谢谢!真的有帮助 –

+0

...你确定应该是一个'='?另外,我不认为你需要额外的括号。 –

1

尝试使用KeyDown事件。

只看KeyDownMSDN

+0

不完全是我在找什么。不管怎么说,多谢拉! –

+0

那么你试图做什么? – nrofis