I would like to set hotkeys in my Windows Forms form. For example, Ctrl + N for a new form and Ctrl + S for save. How would I do this?
To assign a keyboard shortcut do the following: Begin keyboard shortcuts with CTRL or a function key. Press the TAB key repeatedly until the cursor is in the Press new shortcut key box. Press the combination of keys that you want to assign.
Set the button's UseMnemonic property to true and add an ampersand (&) just before the letter in the button's Text property you want to use for the hotkey. The user would press Alt + to activate the hotkey. Show activity on this post. Windows Application and shortcut key are synonyms.
Set
myForm.KeyPreview = true;
Create a handler for the KeyDown event:
myForm.KeyDown += new KeyEventHandler(Form_KeyDown);
Example of handler:
// Hot keys handler void Form_KeyDown(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode == Keys.S) // Ctrl-S Save { // Do what you want here e.SuppressKeyPress = true; // Stops other controls on the form receiving event. } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With