dotnet / dotnet/wpf

KeyBinding `ctor` throws `System.NotSupportedException` on only one key (no modifiers)

Open
#8,639 3 comments 0 reactions 0 assignees View on GitHub
Investigate
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### Description

Currently `InputBindings.Add(new KeyBinding(MyCommand, Key.E, ModifierKeys.None))` throws a `System.NotSupportedException`. This is quite annoying for [several people](https://stackoverflow.com/questions/12074627/keygesture-doesnt-support-only-one-key) and not only that its annoying, its also completely wrong (before you're going to throw me a link to the MSDN remark sections, read further)

Because in XAML I can easily do:
```XAML

```
and it just works. So it IS supported. And to make it clear that the exception is completely wrong take the following code:

```C#
KeyBinding b = new KeyBinding()
{
Command = MyCommand,
Key = Key.E
};
InputBindings.Add(b);
```

which does NOT throw but its ugly to write especially if you have many inputbindings to add. And YES I am aware of the remark section on MSDN. Specially about `With the exception of the function keys and the numeric keypad keys, a valid KeyGesture must contain exactly one Key and one or more ModifierKeys`. But if Im able to do this, just by writing the code slightly different or using XAML then I should also be able to do this using the ctor in code. Especially when I am using the second ctor of `Keybinding` WITHOUT `KeyGesture`. _I haven't looked at the source code but my guess second ctor is going to create a KeyGesture which then throws. Maybe it should do the same which the XAML compiler does as this doesnt throw?_

### Reproduction Steps

```C#
InputBindings.Add(new KeyBinding(MyCommand, Key.E, ModifierKeys.None))

```

### Expected behavior

No exception

### Actual behavior

System.NotSupportedException

### Regression?

_No response_

### Known Workarounds

Either using XAML or
```C#
KeyBinding b = new KeyBinding()
{
Command = MyCommand,
Key = Key.E
};
InputBindings.Add(b);
```

### Impact

_No response_

### Configuration

_No response_

### Other information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.