Перейти к основному содержимому
Версия: 11.0.0

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.

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

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

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.


Source code