I want to provider user with option to set ToolStripMenuItem.ShortcutKeys
via configuration file, so I've figured out that I need somehow transform string
to Keys
.
I've already found how to do that for simple values using Enum.Parse
, but it won't recognize formats like:
Ctrl+i
(with space at the end)i
Ctrl+Alt+Esc
Q: Is there any standardized way of parsing stings (Ctrl+i
) to Keys
?
I'd like to avoid writing my own function that will split text into pieces and then handle each case/special string separately.
The string you see in the Properties window for the ShortcutKeys property is generated by a TypeConverter. You can use that type converter in your own code as well. Like this:
var txt = "Ctrl+I";
var cvt = new KeysConverter();
var key = (Keys)cvt.ConvertFrom(txt);
System.Diagnostics.Debug.Assert(key == (Keys.Control | Keys.I));
Do beware that I
or Ctrl+Alt+Escape
are not valid short-cut keys. KeysConverter will not complain, you'll get the exception when you assign the ShortCutKeys property. Wrap both with try/except to catch invalid config data.
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