Keyboard shortcut for a button

后端 未结 3 1515
情话喂你
情话喂你 2020-12-31 02:59

In C# (Microsoft Visual Studio 2010), how can I assign a keyboard shortcut to a button such as the following?

    private void closeButton_Click(ob         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-31 03:34

    KeyDown is your friend ;) For example, if you want the shortcut key A while Shift is pressed, try this:

        void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.A && e.Shift) 
                // Do something
        }
    

    If you want a "real" keyboard shortcut you can use hooks. Look at Stack Overflow question RegisterHotKeys and global keyboard hooks?.

提交回复
热议问题