Keyboard shortcuts

  • Thread starter Thread starter Mystique
  • Start date Start date
* "Mystique said:
How can i define keyboard shortcuts for a form using the arrow keyboard
keys?

You mean that the arrow keys are part of the shortcuts?
 
Hi Mystique,

You can use only whatever is in Shortcut enumeration.

Values of that enumaration are sumbly bitwise OR of key modifiers and keys.
e.g. the value of Shortcut.Alt0 is equvalent of the value of (Keys.Alt |
Keys.D0)
So here is the problem Keys enumerator has different values for normal 0
(Keys.D0) and the numeric-keypad 0 (Keys.NumPad0). BTW that values are equal
to the virtual keys values.

It is not a problem to create value of Shortcut enum type equal to (Keys.Alt
| Keys.NumPad0), but MenuItem class checks the value that one tries to set
in the Shortcut properties and throws exception.
 
Hi Mystique,

In addition to what I said in my previous post I want to give you and
example that numpad shortcuts are not supported only because they are not
listed in the Shortcut enum.

Assuming that you want to set the shortcut for menuItem2 to Ctrl+NumPad0
put this code in the form cosntructor after InitializeComponent call.

Type t = typeof(MenuItem);
FieldInfo fi = t.GetField("data", BindingFlags.Instance |
BindingFlags.NonPublic);
object miData = fi.GetValue(menuItem2);
t = miData.GetType();
fi = t.GetField("shortcut", BindingFlags.Instance | BindingFlags.NonPublic);
fi.SetValue(miData, (Shortcut)(Keys.Control | Keys.NumPad0));

Now menuItem2 responds to Ctrl+NumPad0

NOTE:
This is only for demonstration purposes.
You don't have to use a code such this in your application. Eventhough that
code works with .NET 1.0 and .NET 1.1 frameworks (and it may work with the
future versions) that is not a workaround that is framework hacking. It uses
internal ManuItem field (data) and even more it uses one internal type
(MenuItemData). Thus, my suggestion is don't use it in a real application it
is not reliable. Those fields and the type may be changed in future.
 
Back
Top