Search code examples
wpfxamlkey-bindingsapostrophe

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?


Solution

  • 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#