Skip to main content
Version: 11.0.x

Keyboard and Hotkeys

Various Controls that implement ICommandSource have a HotKey property that you can set or bind to. Pressing the hotkey will execute the command bound to the Control.

<Menu>
<MenuItem Header="_File">
<MenuItem x:Name="SaveMenuItem" Header="_Save" Command="{Binding SaveCommand}" HotKey="Ctrl+S"/>
</MenuItem>
</Menu>

You can also use the static methods of the HotKeyManager class to set and get hotkeys from code:

InitializeComponent();
HotKeyManager.SetHotKey(saveMenuItem, new KeyGesture(Key.S, KeyModifiers.Control));

Keys and Modifiers

A Hotkey must have one Key and zero or more KeyModifiers. When setting a Hotkey in XAML using the HotKey property, the string will be parsed as a KeyGesture. Enum.Parse is used to parse the key and modifiers but synonyms like Ctrl instead of Control or Win instead of Meta can be used.

Assign number keys to hotkeys

  • A Hotkey must be use D1..D0 or NumPad1..NumPad0. see: Key
  • By binding the same command to two buttons and hiding one button, you can differentiate between a single number on the numpad and a simple Ctrl+number key.
  • If you want to limit command malfunctions caused by pressing numbers on the numpad, you can also use KeyModifiers.
<!--  It's worked fine  -->
<!-- e.g. Ctrl+1 -->
<Button
Command="{Binding CommandX}"
Content="[1]"
HotKey="Ctrl+D1" />
<!-- e.g. You can also use Alt+NumPad1 -->
<Button
Command="{Binding CommandX}"
HotKey="NumPad1"
IsVisible="False" />

<!-- These didn't work -->
<!-- Button.KeyBindings -->
<Button Command="{Binding CommandX}" Content="X">
<Button.KeyBindings>
<KeyBinding Gesture="Ctrl+1" />
</Button.KeyBindings>
</Button>
<!-- Alt+Number -->
<Button Command="{Binding CommandX}" Content="_1" />

Reference

Source code