SoFunction
Updated on 2025-03-07

How to bind shortcut keys to winform

This article describes the method of binding shortcut keys to winform. Share it for your reference. The specific analysis is as follows:

The first type: Alt + * (button shortcut key)

When you set Text properties for button, label, menuStrip and other controls, just add the & key name after the name, such as = "&O". There will be a shortcut key. At this time, press Alt+O to execute the button click event.

The second type: Ctrl+* and other combinations of keys

Set the KeyPreview (Register keyboard events with form) property in WinForm to use key combinations to True;

Then use the KeyDown event of the form (occurred when a key is first pressed).

The following is to register a key event for button1 (CTRL+ENTER)

private void FrmChat_KeyDown(object sender, KeyEventArgs e)
{
  if ( ==  && )
  {
  = true;//Indicates that the KeyPress event has been processed this.button1_Click(null, null);//Call button click event  }
}

I hope this article will be helpful to everyone's C# programming.