在WinForm C#上捕获KeyUp事件

mic*_*kro 8 c# events controls winforms

我试图抓住F5 System.Windows.Forms,因为我写道:

partial class MainForm
{
   (...)
   this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.MainForm_KeyUp);
   (...)
}

public partial class MainForm : Form
{
    (...)

    private void MainForm_KeyUp(object sender, KeyEventArgs e)
    {
        Log("MainForm_KeyUp");
        if (e.KeyCode == Keys.F5)
        {
            RefreshStuff();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但我的事件捕捉看起来不起作用.

你知道如何使用EventKey System.Windows.Forms吗?

Dam*_*ith 12

表单的keypreview属性必须设置为true

当此属性设置为true时,表单将接收所有KeyPress,KeyDown和KeyUp事件.在表单的事件处理程序完成键击处理之后,然后将键击分配给具有焦点的控件.