A managed library to handle global hotkeys in Windows Forms and WPF applications
A managed library to handle global hotkeys in Windows Forms and WPF applications
Easily handle shortcut keys even when your WPF or WinForms app doesn't have focus. Declare hotkeys in XAML with the familiar KeyBinding syntax.
Nuget packages:
Add a reference to NHotkey.dll and NHotkey.WindowsForms.dll. In the file where you want to
handle hotkeys, import the NHotkey.WindowsForms namespace:
using NHotkey.WindowsForms;During initialization, add some hotkeys:
HotkeyManager.Current.AddOrReplace("Increment", ModKeys.Control | ModKeys.Alt | Keys.Add, OnIncrement);
HotkeyManager.Current.AddOrReplace("Decrement", ModKeys.Control | ModKeys.Alt | Keys.Subtract, OnDecrement);EventHandler that will be called
when this hotkey is pressed. For instance: private void OnIncrement(object sender, HotkeyEventArgs e)
{
Value++;
e.Handled = true;
}
private void OnDecrement(object sender, HotkeyEventArgs e)
{
Value--;
e.Handled = true;
}If you want to handle several hotkeys with the same handler, you can check the Name
property of the HotkeyEventArgs:
private void OnIncrementOrDecrement(object sender, HotkeyEventArgs e)
{
switch (e.Name)
{
case "Increment":
Value++;
break;
case "Decrement":
Value--;
break;
}
e.Handled = true;
}Notes regarding the Windows key:
Keys enum does not include the Windows key as a modifier, so NHotkey defines a ModKeys class that
exposes all modifier keys, including the Windows key. You can just use ModKeys instead of Keys for the modifiers.The approach for WPF is very similar to the one for Windows Forms; the exposed API is slightly
different to account for the differences between WinForms and WPF. The WPF version also
supports KeyBindings.
Add a reference to NHotkey.dll and NHotkey.Wpf.dll. In the file where you want to
handle hotkeys, import the NHotkey.Wpf namespace:
using NHotkey.Wpf;During initialization, add some hotkeys:
HotkeyManager.Current.AddOrReplace("Increment", Key.Add, ModifierKeys.Control | ModifierKeys.Alt, OnIncrement);
HotkeyManager.Current.AddOrReplace("Decrement", Key.Subtract, ModifierKeys.Control | ModifierKeys.Alt, OnDecrement);EventHandler that will be called
when this hotkey is pressed.To support applications that use the MVVM pattern, you can also specify hotkeys in XAML using
InputBindings. Just declare KeyBindings as usual, and set the HotkeyManager.RegisterGlobalHotkey
attached property to true:
...
...Known limitations of this feature
HotkeyManager can't detect if you remove a KeyBinding; it only relies on the
attached property being set to true or false. If you want to remove a KeyBinding at runtime,
make sure you set HotkeyManager.RegisterGlobalHotkey to false, otherwise it will
still be registeredKeyBinding at runtime is currently not supported. If
you need to modify a KeyBinding at runtime, you need to set HotkeyManager.RegisterGlobalHotkey
to false, change the key, and set HotkeyManager.RegisterGlobalHotkey to true again.No open issues yet, or sync has not completed.