Introduction
Basically hot keys provide quick access to a feature of software application by pressing finite set of one or more keys. These keys will work even when the specific application is inactive or hidden. Normally implementing hot-keys feature for an application is a complex process. I want to make it simple and it is needed for one of my projects. After spending some time with MSDN, I created the Smart Hot key Handler in C#.NET. It will very useful for .NET based windows application developers; they can use it even they don't have any knowledge on Win32 application and libraries.
Using the Smart Hot Key Handler
Smart Hotkey handler.NET allows Windows applications to subscribe all registered hotkey events with minimum efforts. It supports to register all normal keys and function keys as hot key with or without key modifiers (Alt, Shift, Control and Windows).
You can get the full source code of this handler here.
Follow the steps given below to use the smart hot key handler in a test application.
- Create a new Windows Application from Visual Studio 2008.
- Add Smart Hot key control to Toolbox (if not available), then drag and drop on to your form.
- Open your form code file and add the following line of code at the constructor of the form.
this.hotKey1.HotKeyPressed +=
new SmartHotKey.HotKey.HotKeyEventHandler(hotKey1_HotKeyPressed);
- Add the hot key press event handler.
void hotKey1_HotKeyPressed(object sender, SmartHotKey.HotKeyEventArgs e)
{
MessageBox.Show(e.HotKey);
}
- Register all required hot-keys.
this.hotKey1.AddHotKey("S");
this.hotKey1.AddHotKey("A");
this.hotKey1.AddHotKey("Control+Shift+Alt+U");
this.hotKey1.AddHotKey("Control+Shift+E");
this.hotKey1.AddHotKey("F2");
this.hotKey1.AddHotKey("F9");
this.hotKey1.AddHotKey("Shift+F1");
this.hotKey1.AddHotKey("Shift+End");
this.hotKey1.AddHotKey("End");
- Add the following line of code inside of the
Dispose
method to release the hot key handler.
this.hotKey1.RemoveAllKeys();
this.hotKey1.RemoveKey("Shift+End");
- Now you run and test your application, you can get the following messages when you hit one or more keys on your keyboard.
Sample Outputs
In the same way, you can customize the hot key combination based on your requirements.
Conclusion
I hope this will reduce your development time on hot key implementation for your application. Currently Windows key modifier combination hot-keys are not working properly. If you have any workaround or solution, please share with me.
This is my first article, so I look forward to getting your feedback on grammatical and technical mistakes. It will really help me to improve myself and write more useful articles.
History
- 9th August, 2010: Initial post