All Projects → thomaslevesque → Nhotkey

thomaslevesque / Nhotkey

Licence: apache-2.0
A managed library to handle global hotkeys in Windows Forms and WPF applications

Programming Languages

csharp
926 projects

Projects that are alternatives of or similar to Nhotkey

Sharpscada
C# SCADA
Stars: ✭ 2,043 (+845.83%)
Mutual labels:  wpf
Oxyplot
A cross-platform plotting library for .NET
Stars: ✭ 2,466 (+1041.67%)
Mutual labels:  wpf
Wpf Autocomplete Textbox
An autocomplete TextBox for WPF
Stars: ✭ 210 (-2.78%)
Mutual labels:  wpf
Netsparkle
NetSparkle is a C# software update framework for .NET developers compatible with .NET Core, WinForms, WPF, and Avalonia; uses Ed25519 or DSA signatures! View basic usage here in the README or visit our website for code docs. 2.0 is stable but still in preview.
Stars: ✭ 154 (-28.7%)
Mutual labels:  wpf
Fontawesome.sharp
A library for using Font Awesome in WPF & Windows Forms applications
Stars: ✭ 185 (-14.35%)
Mutual labels:  wpf
Caliburn.micro
A small, yet powerful framework, designed for building applications across all XAML platforms. Its strong support for MV* patterns will enable you to build your solution quickly, without the need to sacrifice code quality or testability.
Stars: ✭ 2,404 (+1012.96%)
Mutual labels:  wpf
Papercut Smtp
Papercut SMTP -- The Simple Desktop Email Server
Stars: ✭ 2,094 (+869.44%)
Mutual labels:  wpf
Serial Assistant
一款使用 C# 及 WPF 框架编写的串口调试助手,界面优雅、简洁,易于使用。
Stars: ✭ 212 (-1.85%)
Mutual labels:  wpf
Netprints
Visual programming for .NET inspired by Unreal Engine's Blueprints
Stars: ✭ 194 (-10.19%)
Mutual labels:  wpf
Code Samples
Just some code samples for MahApps and other experiments...
Stars: ✭ 205 (-5.09%)
Mutual labels:  wpf
Microsoft.maui.graphics
Stars: ✭ 160 (-25.93%)
Mutual labels:  wpf
Signalchat
WPF-MVVM instant messaging application using SignalR
Stars: ✭ 172 (-20.37%)
Mutual labels:  wpf
Office Ribbonx Editor
An overhauled fork of the original Custom UI Editor for Microsoft Office, built with WPF
Stars: ✭ 205 (-5.09%)
Mutual labels:  wpf
Github Actions For Desktop Apps
This repo contains a sample WPF application to demonstrate how to create CI/CD pipelines using GitHub Actions.
Stars: ✭ 156 (-27.78%)
Mutual labels:  wpf
Pixieditor
PixiEditor is a lightweight pixel art editor made with .NET 5
Stars: ✭ 210 (-2.78%)
Mutual labels:  wpf
Modernwpf
Modern styles and controls for your WPF applications
Stars: ✭ 2,610 (+1108.33%)
Mutual labels:  wpf
Awesome Wpf
A collection of awesome WPF resources, libraries and UI controls.
Stars: ✭ 196 (-9.26%)
Mutual labels:  wpf
Dmskin Cloudmusic
网易云音乐-用WPF来做网易云音乐客户端会怎么样?
Stars: ✭ 213 (-1.39%)
Mutual labels:  wpf
Tumblthree
A Tumblr Backup Application
Stars: ✭ 211 (-2.31%)
Mutual labels:  wpf
Screentogif
🎬 ScreenToGif allows you to record a selected area of your screen, edit and save it as a gif or video.
Stars: ✭ 16,066 (+7337.96%)
Mutual labels:  wpf

NHotkey

NuGet version NuGet version NuGet version AppVeyor build

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].