Hotkey for a button in a C # Windows application

How to make a hotkey for a button in a Windows C # application

+6
c #
source share
8 answers

Replace the form ProcessCmdKey . If you find a keystroke that you like, call the same method as the button.

 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (keyData == (Keys.Control | Keys.X)) { DoSomething(); return true; } return base.ProcessCmdKey(ref msg, keyData); } private void button1_Click(object sender, EventArgs e) { DoSomething(); } private void DoSomething() { MessageBox.Show("hi!"); } 

EDIT : The Jay method is better if you can find the appropriate mnemonics.

+15
source share

Set the UseMnemonic property to true and add an ampersand (&) immediately before the letter in the text property of the button that you want to use for the hotkey.

The user will press Alt + to activate the hotkey.

+9
source share

Windows applications and keyboard shortcuts are synonymous. People prefer to use keyboard shortcuts for faster activity. Suppose you are in a text box and you want to press F5 to reload the form. You do not want to move the mouse cursor. So what you will do is that you add the โ€œKey Downโ€ event to the text box. But if you are in another text field or in a button, this will not work. This way you add the "Key Down" event to your form.

But you need to enable the property for proper operation, etc.

 private void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.F5) { btnRefresh_Click(null, null); } } 

But the golden trick in the form properties window makes the following changes:

 KeyPreview = True 

Enjoy the programming.

+6
source share

In addition, each form has AcceptButton and CancelButton properties. If you intend to restrict the use of keyboard shortcuts for input and output buttons, which, in turn, should call certain buttons, I would recommend that you consider using these properties.

+1
source share

I think this is what you want:

 private void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { //Do whatever you want here... } } 

Of course, you can change Keys.Enter to any desired key.

0
source share
 this.KeyDown += new KeyEventHandler(Form1_KeyDown); void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.Alt && e.KeyCode == Keys.W) { //do something } } 
0
source share

Based on Micheal, respond to this more elegant version if you don't use combination keys:

 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { switch(keyData) { case Keys.F2: // do something... return true; case Keys.F3: // do something... return true; case Keys.F4: // do something... return true; default: return base.ProcessCmdKey(ref msg, keyData); } } 
0
source share

although this post is super old, itโ€™s an easier way to do this, the code is pretty poor, but it does the job very well.

  private void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { button1.PerformClick(); } 
-one
source share

All Articles