Winforms中的关键控制事件 [英] Key control events in winforms

查看:100
本文介绍了Winforms中的关键控制事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按下GUI中的一个按钮,按下按钮后会显示一条消息.但是keydown事件无法正常工作.取而代之的是,它同时使用空格键"和"Enter"键以及WinForms中的设置键.

我尝试过的事情:

//如前所述

I press a button in GUI.Button displays a message after I press it. But the keydown event does not work properly. Instead, it takes both "spacebar" and "Enter" keys along with the set keys in WinForms

What I have tried:

//As mentioned

this.KeyPreview = true;


//


//

this.KeyDown += new KeyEventHandler(Form1_KeyDown);



//在键盘上



//IN KEYDOWN

if (e.KeyCode == Keys.H)
         {
             button1.PerformClick();
         }

推荐答案

如果我将该代码插入测试应用程序中:
If I insert that code into a test app:
public frmMain()
    {
    InitializeComponent();
    this.KeyPreview = true;
    this.KeyDown += new KeyEventHandler(frmMain_KeyDown);
    }


private void frmMain_KeyDown(object sender, KeyEventArgs e)
    {
    if (e.KeyCode == Keys.H)
        {
        myButton.PerformClick();
        }
    }


private void MyButton_Click(object sender, EventArgs e)
    {
    Console.WriteLine("Done");
    }

然后,每当我在窗体具有焦点的位置上按键盘上的"H"键,它就会在输出窗格中打印"Done"(这是我的期望.).
因此,鉴于我复制并粘贴了您的代码,您在做什么与我不同...

Then every time I press "H" on the keyboard while the form has the focus, it prints "Done" in the output pane - which is what I expect.
So what are you doing that is different to me, given that I copy''n''pasted your code...


这篇关于Winforms中的关键控制事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆