To customize the shortcut (which are not present in the System.Windows.Forms.Shortcut Enum), you need to override the ProcessCmdKey event of the form and handle the needed keys. The below example has a menu item with name “item” and the shortcut Ctrl + Alt + 0 has been set by handling the ProcessCmdKey event. The Click event of the menu item is simulated.
//Override this method in the form which have the menu. This will only trigger when the form is on focus.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.Alt | Keys.D0)) // Ctrl + Alt + 0
{
if (this.Menu != null && this.Menu.MenuItems.Count > 0)
this.Menu.MenuItems['item'].PerformClick(); // triggers click event.
}
return base.ProcessCmdKey(ref msg, keyData);
}
Instead of MenuItem, you can utilize ToolStripMenuItem which has plenty of features and all kind of shortcuts are available.
Share with