Click here to Skip to main content
15,891,513 members
Articles / Programming Languages / C#
Tip/Trick

HotKey API.NET

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
17 Apr 2014CPOL2 min read 10.4K   231   11  
Simple API to Register/Listen for HotKey in .NET applications

Introduction

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.

Background

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

Using the Code

C++
//
// 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)
              .WithControlPressed()
              .WithAltPressed()
              .GetHotKey();
//
// Well, registering into the SO its also very easy.
// 

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

var theListener = new HotKeyListener();
theListener.StartListening();
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

License

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


Written By
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.

Comments and Discussions

 
-- There are no messages in this forum --