简体   繁体   English

在C#中通过按键激活按钮

[英]Activate button by key press in C#

I am making a simple form with a lot of buttons, and I want each button assigned with a letter(ex: q,w,t,y) and when I press that key on the keyboard, that button is 'pressed', like I've clicked on it, and if I press it three times, it's like I've clicked on it three times - you get the idea. 我正在制作带有许多按钮的简单表单,我希望为每个按钮分配一个字母(例如q,w,t,y),当我按键盘上的该键时,该按钮会被“按下”,例如我已经单击了它,如果我按了三下,就好像我已经单击了三下-您就知道了。 I've tried with this: 我已经试过了:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Q: 
                    Q_Button();
                    return true;
...

It's working but only the first time you press the key - and then the application is dull, no matter how many times you press it again it's not working. 它可以正常工作,但是只有在您第一次按下该键时,然后该应用程序才会变暗,无论您再按几次它都无法正常工作。 Also it's not working for the multiple presses scheme, cos it's only working once - the first time. 同样,它不适用于多重印刷方案,因为它只能工作一次-第一次。 I need some ideas here. 我在这里需要一些想法。 Thanks in advance. 提前致谢。

Description 描述

Assuming you are doing Windows Forms you should use the buttons PerformClick method. 假设您正在执行Windows窗体,则应使用按钮PerformClick方法。

More Information 更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM