简体   繁体   中英

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. 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.

More Information

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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