Search code examples
c#wpf.net-coreinputbinding

Create a simple, unmodified key binding in WPF (.net core 3.1 /.net 5)


This is an old issue, which was already asked here (Create a simple, unmodified key binding in WPF) but the answer will not work for me. I still have a problem.

I use .net core 3.1 with WPF and I want to define a KeyBinding to an unmodified key by code.

In XAML

<Window.InputBindings>
   <KeyBinding Command="{Binding MyRoutedCommand}" Key="H" />
</Window.InputBindings>

it works like a charm. But if I try to define the KeyBinding in code,

KeyBinding b = new KeyBinding()
{
   Command = MyRoutedCommand,
   Key = Key.H
};
InputBindings.Add(b);

I get a "NotSupported" exception for the gesture None+H directly on starting the App.

Original exception (german, Visual Studio 2019):

NotSupportedException: Die Kombination aus Taste und Modifizierer "None+H" wird für KeyGesture nicht unterstützt.

Does anybody know a reason, why XAML is working and setting the InputBinding by code fails?

Kind regards, Christof

PS:

Based on some comments I found following code line in the code behind constructor.

  InputBindings.Add(new KeyBinding(MyRoutedCommand, Key.H, ModifierKeys.None));

If I remove this line, the exception is gone. So I think the reason for the exception is found.

Solution (added on 2020-08-09):

The code I mentioned in the question originaly was right. Only the line I found in the code behind file was the reason for the exception. If you compare the code lines you see, that there are two different KeyBinding constructors are used.

The problem is, that the parameterless KeyBinging() constructor and the KeyBinding(key, modifier) constructor work in a different way.

The constructor with key and modifier parameter will create a KeyGesture internally. And the KeyGesture will throw the exception if you try to use a "printable" key without a modifier key. Even the shift key is not supported by the KeyGesture. So None + H and Shift + H are not valid.

But in the KeyBinding class itself, it is no problem to use these key combinations. If you need to define a KeyBinding to a "printable" key, just use the parameterless KeyBiding constructor and add the RoutedCommand and the key manually before you add the KeyBinding to the InputBindings. This is shown in the code above.

I have to say "Thank you" for the comments. Without the help I wouldn't have seen my mistake in the code behind file.


Solution

  • NotSupportedException: Die Kombination aus Taste und Modifizierer "None+H" wird für KeyGesture nicht unterstützt.

    Both of your solutions work for .NET Framework, as well as .NET Core 3.1. This exception is clearly about KeyGesture. You instantiate a KeyGesture with a None modifier like below when creating the key binding and that will throw the NotSupportedException. Removing this line or assigning a modifier will resolve it.

    InputBindings.Add(new KeyBinding(MyRoutedCommand, Key.H, ModifierKeys.None));
    

    As a reference from MSDN about KeyGesture and modifier keys:

    In most cases, a KeyGesture must be associated with one or more ModifierKeys. The exceptions to this rule are the function keys and the numeric keypad keys, which can be a valid KeyGesture by themselves.