Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keyboard shortcut for a button

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

    private void closeButton_Click(object sender, EventArgs e)
    {
        // Close the program
        this.Close();
    }

I know I can use the "&" character in the button's Text and create an Alt - n shortcut, but I'd like to create a single keypress shortcut, such as c to execute the above.

like image 379
Tim S Avatar asked Oct 21 '11 21:10

Tim S


People also ask

What is Ctrl F3?

Ctrl+F3. Paste the contents of the Spike. Ctrl+Shift+F3. Copy the selected formatting.

What is Ctrl F8?

Ctrl+F8: Performs the Size command when a workbook is not maximized. Alt+F8: Displays the Macro dialog box to create, run, edit, or delete a macro. F9. F9: Calculates all worksheets in all open workbooks.

What does Ctrl F 11 do?

Ctrl + F11 as the computer is starting to access the hidden recovery partition on many Dell computers. Pressing F11 by itself accesses the hidden recovery partition on eMachines, Gateway, and Lenovo computers.

What does Ctrl P do?

In ManualTest the keyboard shortcut "ctrl+p" is used for Printing. The same shortcut is also used for the menu item, EDIT -> Mark as VP.


1 Answers

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

like image 138
dknaack Avatar answered Oct 11 '22 23:10

dknaack