Can you add an ESC Key shortcut?

I have a Menustrip element on my Form and it closes my Form when I click it. I want to make a shortcut for my Menustrip item โ€œESCโ€, but in the โ€œShorcutKeyโ€ settings it has no option for ESC, is there any way to make it make it โ€œESCโ€,? I need to show on the Menustrip element that the shortcut key is "ESC".

Doing this does NOT work:

 menuStripItem.ShortcutKeys = Keys.Escape; 
+6
source share
2 answers

I do not think that there is a direct solution to this issue. You can use this workaround, set ShortcutKeyDisplayString to code

 menuStripItem.ShortcutKeyDisplayString = "ESC"; 

Inside the KeyDown form, check if the ESC and Close() form are clicked.

+6
source

Winforms are picky about the selected keyboard shortcut. The rule is that it must be a function key (F1-F12) or another key with Keys.Control or Keys.Alt . The bigger intention here is that you cannot accidentally replace a regular key that can be used, say, in a TextBox . The Escape button usually includes a dialog cancel button.

Keys.Escape pretty special; Alt + Escape and Ctrl + Escape cannot work, as they are global keyboard shortcuts in Windows.

Therefore, you cannot use the ShortcutKeys property; you need to recognize the Escape key in different ways. It runs easily in your Form class by overriding the ProcessCmdKey() method. Paste this code into the form:

 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (keyData == Keys.Escape) { this.Close(); return true; } return base.ProcessCmdKey(ref msg, keyData); } 
+8
source

Source: https://habr.com/ru/post/951744/


All Articles