本文介绍了尝试检测按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一种方法,该方法可以检测何时按下了一个键,但是它不起作用!这是我的代码

I made a method that detects when a key is pressed, but its not working! Heres my code

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,但是它说由于它是一个方法组而不能分配给按键检测"

I also tried to make a keyeventhandler but, it sais "cannot assign to key detect because it is a method group"

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

推荐答案

使用像这样的按键事件:

Use keypress event like this:

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

这篇关于尝试检测按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 06:11