PhoenixTools.NHotkey.WindowsForms 3.1.5

dotnet add package PhoenixTools.NHotkey.WindowsForms --version 3.1.5
                    
NuGet\Install-Package PhoenixTools.NHotkey.WindowsForms -Version 3.1.5
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PhoenixTools.NHotkey.WindowsForms" Version="3.1.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PhoenixTools.NHotkey.WindowsForms" Version="3.1.5" />
                    
Directory.Packages.props
<PackageReference Include="PhoenixTools.NHotkey.WindowsForms" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add PhoenixTools.NHotkey.WindowsForms --version 3.1.5
                    
#r "nuget: PhoenixTools.NHotkey.WindowsForms, 3.1.5"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=PhoenixTools.NHotkey.WindowsForms&version=3.1.5
                    
Install PhoenixTools.NHotkey.WindowsForms as a Cake Addin
#tool nuget:?package=PhoenixTools.NHotkey.WindowsForms&version=3.1.5
                    
Install PhoenixTools.NHotkey.WindowsForms as a Cake Tool

NHotkey

Forked from https://github.com/thomaslevesque/NHotkey

Add Avalonia framework support.

NuGet version NuGet version NuGet version NuGet version

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:

Windows Forms usage

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", Keys.Control | Keys.Alt | Keys.Add, OnIncrement);
    HotkeyManager.Current.AddOrReplace("Decrement", Keys.Control | Keys.Alt | Keys.Subtract, OnDecrement);
  • the first parameter is an application-defined name for the hotkey; it can be anything you like, as long as it's unique;
  • the second parameter is the combination of keys for which you want to register a hotkey;
  • the last parameter is a delegate of type EventHandler<HotkeyEventArgs> 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;
    }

WPF usage

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);
  • the first parameter is an application-defined name for the hotkey; it can be anything you like, as long as it's unique;
  • the second and third parameters are the key and modifiers for which you want to register a hotkey;
  • the last parameter is a delegate of type EventHandler<HotkeyEventArgs> 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:

    ...
    <Window.InputBindings>
        <KeyBinding Gesture="Ctrl+Alt+Add" Command="{Binding IncrementCommand}"
                    HotkeyManager.RegisterGlobalHotkey="True" />
        <KeyBinding Gesture="Ctrl+Alt+Subtract" Command="{Binding DecrementCommand}"
                    HotkeyManager.RegisterGlobalHotkey="True" />
    </Window.InputBindings>
    ...

Known limitations of this feature

  • the 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 registered
  • changing the keys or modifiers of a KeyBinding 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.

Avalonia usage

In-code usage and limitations are similar to WPF.

Namespace :

    using NHotkey.Avalonia;

AXAML syntax:

    <Window 
    xmlns:avalonia="clr-namespace:NHotkey.Avalonia;assembly=NHotkey.Avalonia"
    ... >
    ...
    <Window.KeyBindings>
        <KeyBinding Gesture="Ctrl+Alt+N" Command="{Binding NegateCommand}"
                    avalonia:GlobalKeyBinding.RegisterGlobalHotkey="True" />
        <KeyBinding Gesture="Ctrl+Alt+T" Command="{Binding TestCommand}"
                    avalonia:GlobalKeyBinding.RegisterGlobalHotkey="True" />
    </Window.KeyBindings>
    ...
Product Compatible and additional computed target framework versions.
.NET net6.0-windows7.0 is compatible.  net7.0-windows was computed.  net8.0-windows was computed.  net8.0-windows7.0 is compatible.  net9.0-windows was computed. 
.NET Framework net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.1.5 81 2/28/2025
3.1.3 77 2/28/2025