百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
N

NHotkey

> 编程语言
开源

一个管理库,用于处理 Windows Forms 和 WPF 应用程序中的全局快捷键

342 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

一个管理库,用于处理 Windows Forms 和 WPF 应用程序中的全局快捷键

NHotkey

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:

  • for WPF: NHotkey.Wpf
  • for Windows Forms: NHotkey.WindowsForms

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:

csharp
    using NHotkey.WindowsForms;

During initialization, add some hotkeys:

csharp
    HotkeyManager.Current.AddOrReplace("Increment", ModKeys.Control | ModKeys.Alt | Keys.Add, OnIncrement);
    HotkeyManager.Current.AddOrReplace("Decrement", ModKeys.Control | ModKeys.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 that will be called when this hotkey is pressed. For instance:
csharp
    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:

csharp
    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:

  • The standard 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.
  • Keep in mind that the operating system reserves some combinations involving the Windows key. Trying to register such combinations as hotkeys will fail.

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:

csharp
    using NHotkey.Wpf;

During initialization, add some hotkeys:

csharp
    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 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:

xml
    ...
    
        
        
    
    ...

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.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C#csharpdotnetglobal-hotkeyswindows

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言