Click here to Skip to main content
13,054,329 members (117,863 online)
Click here to Skip to main content
Add your own
alternative version


11 bookmarked
Posted 17 Apr 2014


, 17 Apr 2014
Rate this:
Please Sign up or sign in to vote.
Simple API to Register/Listen for HotKey in .NET applications


Hi guys, it's gonna be quick since it's just a small contribution.

Here I developed a simple API that makes super easy to register/listen for hot-keys in any .NET application

When I first started with this component I faced myself with this question, "How do I do to set up a HotKey from a Class Library project?" because I'm aware that System.Windows.Forms has something to do about this, you can also listen for the keydown, but the application I was developing just had a From in order to input some configurations, after a "Play" button it was everything behind it. So investigating Pinvoke I saw that it was possible to register a HotKey via RegisterHotKey method, and also listen for WM_HOTKEY via SetWindowsHookEx.

After playing a while I started to abstract this on an independent solution that makes it super easy to treat with this issue.

I think that the way that someone can be benefited with this its because they simply have to attach this up, simply register any key combination and get to listen the event, ready to solve anything else that the consumer application's problem is. Making the Registration/Listening of HotKeys not one more issue to solve.


You can also search for this in Nugget, the API is called HotKeysAPI.

Using the Code

// This is how you can create the specified hotKey

var factory = new HotKeyFactory(); 
int keyCode = 77; //If I am not mistaken 77 is the keyCode for 'M'
var myHotKey = factory.CreateHotKey(keyCode)
// Well, registering into the SO its also very easy.

var hotKeyRecorder = new HotKeyRecorder();
// The only thing that's left is set up out ears to listen for that.

var theListener = new HotKeyListener();
theListener.OnHotKeyPressed += MyHotKeyPressedEventHandler

The hotKey registration uses the RegisterHotKey API from user32. And the listening works via SetWindowsHookEx to the WM_HotKey window's message, after that the HotKey struct is reconstructed and notified to the Listener's instances via OnHotKeyPressed event.

This is not all the flexibility it has, but it is better if you check that yourself in the src. It's everything absolutly interfaced in case you need to unit test components with this.

Well guys, I hope you to find it as useful as I do. HF


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


About the Author

Martin Solovey
Software Developer Currently Independant
Argentina Argentina
I took my first step on development with 15 years old at ORT High School in Buenos Aires.
After my graduation I started my professional career developing with Microsoft technologies such as .NET (C# & SQL Server) mostly web development.

Nowadays, after more than 4 years as a developer, luckily there is not a single day that I stop learning about development, best agile practices & patterns and cutting edge technologies.

I am actually working with a few friends, starting with small software factory company.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 17 Apr 2014
Article Copyright 2014 by Martin Solovey
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid