In my wpf app I've received requests to create hotkeys using the AltGr
key, such as AltGr+O
, AltGr+M
, AltGr+L
, AltGr+M
etc, with the reason of AltGr
key chosen being that it's simply closer to those letters on the keyword, making it easier to press these hotkeys on a larger-than-conventional keyboard.
Currently, the existing hotkeys are parsed in like so.
var input = ("HotkeyDoesA=ALT+A").Split('=');
if ( input[1].Contains('+') )
{
KeyGestureConverter converter = new KeyGestureConverter();
var gesture = converter.ConvertFromInvariantString(input[1]) as KeyGesture;
collection.Add(new HotKey()
{
CommandParameter = input[0],
KeyGesture = gesture
}
}
public class HotKey
{
public Key Key { get; set; }
public KeyGesture KeyGesture { get; set; }
public string CommandParameter { get; set; }
}
Collection is just a collection of HotKey objects. With the method above, this is usually fine for hotkeys that contain Alt
or Ctrl
because ConvertFromInvariantString("ALT+A")
will simply convert the string into a KeyGesture
item where A
is the Key
and Alt
is the ModifierKeys. But in System.Windows.Input
the ModifierKeys
only has a few options.
namespace System.Windows.Input
{
//
// Summary:
// Specifies the set of modifier keys.
[Flags]
[TypeConverter(typeof(ModifierKeysConverter))]
[ValueSerializer(typeof(ModifierKeysValueSerializer))]
public enum ModifierKeys
{
//
// Summary:
// No modifiers are pressed.
None = 0,
//
// Summary:
// The ALT key.
Alt = 1,
//
// Summary:
// The CTRL key.
Control = 2,
//
// Summary:
// The SHIFT key.
Shift = 4,
//
// Summary:
// The Windows logo key.
Windows = 8
}
}
KeyGesture
is
namespace System.Windows.Input
{
//
// Summary:
// Defines a keyboard combination that can be used to invoke a command.
[TypeConverter(typeof(KeyGestureConverter))]
[ValueSerializer(typeof(KeyGestureValueSerializer))]
public class KeyGesture : InputGesture
{
...public KeyGesture(Key key);
...public KeyGesture(Key key, ModifierKeys modifiers);
...public KeyGesture(Key key, ModifierKeys modifiers, string displayString);
...public string DisplayString { get; }System.Windows.Input.Key.None.
...public Key Key { get; }
...public ModifierKeys Modifiers { get; }
...public string GetDisplayStringForCulture(CultureInfo culture);
...public override bool Matches(object targetElement, InputEventArgs inputEventArgs);
}
}
Is there a workaround for this?