Click here to Skip to main content
13,590,079 members
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 30 Jun 2016
Licenced CPOL

Defining System-wide hotkeys in .NET

, 30 Jun 2016
Rate this:
Please Sign up or sign in to vote.
Using the Windows API to define systemwide hotkeys


When I created an application which needed to be notified whenever a combination of keys is pressed, I noticed there is no standard solution provided for this usecase.

My goal was to write a library that allowed to define hotkeys and send notifications to the subscribed objects even when the application has no focus, is minimized, etc.

I simplified my library to demonstrate a possible approach.

Using the Code

The project is composed of two assemblies:

  • Hotkeys.dll (class library)
  • HotKeyTest.exe (WPF test project)


Eums.cs contains Keys and Modifiers enumerations as provided in System.Windows.Input.

WinAPI.cs contains two methods needed for our usecase:

  • RegisterHotKey
  • UnregisterHotKey

HotkeyListener.cs encapsulates the interaction with the WinAPI to provide a facade between the WinAPI and the code consuming the HotKeys library code.

HotKey.cs encapsulates the definition of a HotKey namely Modifiers (4bits) and Key. Each unique combination gets a unique ID, calculated as follows: cast both enums to integers, shift the Key number with 4 positions (modifiers are composed out of 4 bits) and add the resulting integers.

HotKeyEventArgs is a specialisation of EventArgs containing:

  • definition of the pressed hotkey
  • the time it was pressed


Contains a simple WPF application for testing purposes.

Control + Shift + N is defined by default, by clicking the button, you can add Control + K as a shortcut.

Using the library is as simple as getting an instance of the HotKeyListener class through the singleton pattern + supply the hotkeys you are interested in.

   .listenTo(HotKeys.ModifierKeys.Control | HotKeys.ModifierKeys.Shift, Keys.N, this.Hk_press);

Points of Interest

The HotKey class contains some code to ensure that the same handler can't subscribe twice for an event.

In the HotKeyListener class, I had to use ComponentDispatcher to have WPF play nicely with the WinAPI.


  • 30/06/2016: Initial version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Lorenzo A.
Software Developer
Belgium Belgium
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionHotKeyListener seems not to work Pin
Member 1336303519-Apr-18 22:20
memberMember 1336303519-Apr-18 22:20 
PraiseGood Pin
herves3-Jul-16 23:05
memberherves3-Jul-16 23:05 
Questionnice library Pin
kaos_1211-Jul-16 12:32
memberkaos_1211-Jul-16 12:32 
AnswerRe: nice library Pin
Lorenzo A.2-Jul-16 6:49
memberLorenzo A.2-Jul-16 6:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03-2016 | 2.8.180618.1 | Last Updated 30 Jun 2016
Article Copyright 2016 by Lorenzo A.
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid