ifinlay
ifinlay

Reputation: 651

How do I create a XAML KeyBinding for CTRL+'

In a WPF MVVM application I need to trigger a command when CTRL+' (control and apostrophe) is pressed. None of the following will compile...

<KeyBinding Modifiers="CTRL" Key="'" Command="{Binding MyCommand}"/>
<KeyBinding Modifiers="CTRL" Key="\'" Command="{Binding MyCommand}"/>
<KeyBinding Modifiers="CTRL" Key="&apos;" Command="{Binding MyCommand}"/>
<KeyBinding Gesture="CTRL+'" Command="{Binding MyCommand}"/>
<KeyBinding Gesture="CTRL+&apos;" Command="{Binding MyCommand}"/>
<KeyBinding Gesture="CTRL+\'" Command="{Binding MyCommand}"/>

So how can this key combination be achieved?

Upvotes: 1

Views: 933

Answers (2)

mm8
mm8

Reputation: 169330

An apostrophe is not a key. It's a character that is eventually mapped to a key depending on the input device. OemQuestion works on my keyboard:

<KeyBinding Modifiers="CTRL" Key="OemQuestion"  Command="{Binding MyCommand}"/>

...but you may be better off handling the PreviewTextInput event if you really want to detect when an apostrophe is typed in:

How to detect when (forward) slash key is pressed in OEM keys C#

Upvotes: 1

zaphod-ii
zaphod-ii

Reputation: 524

The key code for apostrophe is OemQuotes. This syntax should do the trick

   <KeyBinding Command="{Binding Command}"
          Modifiers="CTRL" Key="OemQuotes" />

Upvotes: 0

Related Questions