0

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?

0x464e
  • 5,948
  • 1
  • 12
  • 17
user3613025
  • 373
  • 3
  • 10
  • From memory, isn't the right alt key detected as ctrl+alt? – DavidG Jul 06 '23 at 12:09
  • What is the problem? Parsing `"AltGr+A"` string? Simply `string.Replace("AltGr", "Alt")`. Or do you want to distinguish left `"ALT+A"` hotkey from `"ALT GR+A"` hotkey? – Sinatr Jul 06 '23 at 12:12
  • @DavidG, didn't know that, [related](https://stackoverflow.com/q/726634/1997232). Then `string.Replace("AltGr", "Alt+Control")` should do. – Sinatr Jul 06 '23 at 12:18
  • @Sinatr I need to create a `KeyBinding` with that associates a `KeyGesture` with a `ICommand`. Both `KeyBinding` and `KeyGesture` take enums `ModifierKeys` which can only contain any 4 of the ModifierKey enums above, meaning I'm not able to use `AltGr` as a modifier key. – user3613025 Jul 06 '23 at 13:50
  • 1
    The `ModifierKeys` enum has [FlagsAttribute](https://stackoverflow.com/q/8447/1997232), means you can combine more than one values `ModifierKeys.Alt | ModifierKeys.Control` to store `AltGr` modifier. – Sinatr Jul 06 '23 at 13:53
  • That is it. You've actually done it, legend. Please update it as your answer. :) – user3613025 Jul 06 '23 at 14:50

0 Answers0