Click here to Skip to main content
15,890,438 members
Articles / Desktop Programming / Windows Forms

Smart Hotkey Handler .NET

Rate me:
Please Sign up or sign in to vote.
4.45/5 (15 votes)
9 Aug 2010CPOL2 min read 60.2K   2K   56   28
Tutorial about Smart Hot Key Handler .NET control library

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.

  1. Create a new Windows Application from Visual Studio 2008.
  2. Add Smart Hot key control to Toolbox (if not available), then drag and drop on to your form.
  3. Open your form code file and add the following line of code at the constructor of the form.
    C#
    this.hotKey1.HotKeyPressed += 
    	new SmartHotKey.HotKey.HotKeyEventHandler(hotKey1_HotKeyPressed); 
  4. Add the hot key press event handler.
    C#
    void hotKey1_HotKeyPressed(object sender, SmartHotKey.HotKeyEventArgs e)
    {
            MessageBox.Show(e.HotKey);
    } 
  5. Register all required hot-keys.
    C#
    //normal keys
    this.hotKey1.AddHotKey("S");
    this.hotKey1.AddHotKey("A");
        
    //normal keys with keymodifiers
    this.hotKey1.AddHotKey("Control+Shift+Alt+U");
    this.hotKey1.AddHotKey("Control+Shift+E");
    
    //function keys
    this.hotKey1.AddHotKey("F2");
    this.hotKey1.AddHotKey("F9");
    
    //function keys with keymodifiers
    this.hotKey1.AddHotKey("Shift+F1");
    
    //other keys[Home,End,PageDown,PageUp and Delete]
    this.hotKey1.AddHotKey("Shift+End");
    this.hotKey1.AddHotKey("End"); 
  6. Add the following line of code inside of the Dispose method to release the hot key handler.
    C#
    //remove all keys
    this.hotKey1.RemoveAllKeys();
    
    //remove the specific hot key
    this.hotKey1.RemoveKey("Shift+End"); //you can call this method 
    				//wherever you want to remove a hotkey.
    		//It will release the hotkey handler from your application.
  7. 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

S.JPG

Nkeymod.JPG

F2.JPG

ShiftF1.JPG

ShiftEnd.JPG

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

License

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


Written By
Architect FE
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionAn issue found Pin
Member 130031381-Dec-21 18:57
Member 130031381-Dec-21 18:57 
Questionhot key block key everywhere Pin
Predator7519-Sep-19 23:38
professionalPredator7519-Sep-19 23:38 
QuestionVery well Pin
Yves30-Jan-18 5:31
Yves30-Jan-18 5:31 
QuestionQuick Help guide for this tool Pin
Willy Kimura7-Aug-15 0:38
professionalWilly Kimura7-Aug-15 0:38 
Hi everyone!

This small guide will help you in using this nice little component in terms of adding keys. Having done some bit of work with this tool, there are many great functionalities that you can embed into your applications from a global perspective within Windows Forms applications. For example, you can be able to set a combination of keys like CTRL + F2 to call a particular function in your app... say, open a file if it's a Text Editor or show a MessageBox to your users. The only important thing is that you test to see if the keys do actually work because some applications usually provide to their users shortcut keys such as CTRL + N to, for example, create a new file or open a new TAB like in Google Chrome or Firefox, which becomes very difficult to be overridden because they are not from a global level but from an application level, so always be testing any key combinations that you've implemented once you've run your app. Global level means that you are able to set a combination of keys that will be executed even when your application is not the currently active one being used by your users, while Application level means that your application needs to be active in order to detect the combination of keys that you have set. A good example is whenever you press the Windows key or the Start key, the Start menu pops up (if on Windows 7 and earlier), or the Start Screen shows up (if on Windows 8 and later). This is a global level key detection mechanism. This is different as compared to Application level, where you application needs to be active in order for the particular set or combination of keys to be detected or handled. A nice example is when you use the Form KeyDown or KeyPress events. You can only perform the key combinations coded when your form is active, otherwise, the keys won't be detected at all. What now this tool comes in and does is that it is able to detect the Global level events while your application is running, and mind you, it does not need to be the currently active one. This is because it intercepts or taps into the messages sent from the Keyboard to any active application, even while in the Desktop, making it have a sort of pervasive or always active design in whatever application you're using at the moment. This is super convenient and easy especially for those times when e.g. you want your application to detect if a user has copied text to the clipboard or wants to access a particular feature from your application while using another application. That's what makes this small tool stand out and majorly because all you have to do is add the keys or remove them. All the complexities of handling the keyboard hooks has been abstracted from you. All you need to do is drop the keys as a String, like: "Control + F2" together with the checking event and the procedure/function or code to be called, and it's all done for you.

To refer to the list of required keys that can be represented as String inputs in SmartHotKey and many other Hotkey applications that hook to the keyboard, just go to: https://msdn.microsoft.com/en-us/library/system.windows.forms.keys%28v=vs.110%29.aspx[^]

You will be able to refer any key on that page from SmartHotKey like this:
In C#:
C#
AddHotKey("LWin"); //for the WINDOWS key or commonly named START key

C#
AddHotKey("Back"); //for the BACKSPACE key

C#
AddHotKey("CapsLock"); //for the CAPS-LOCK key

C#
AddHotKey("NumLock"); //for the NUM-LOCK key

C#
AddHotKey("Scroll"); //for the SCROLL-LOCK key

C#
AddHotKey("F1"); //for the F1 Function key


In VB.NET:
VB
AddHotKey("LWin") 'for the WINDOWS key or commonly named START key

VB
AddHotKey("Back") 'for the BACKSPACE key

VB
AddHotKey("CapsLock") 'for the CAPS-LOCK key

VB
AddHotKey("NumLock") 'for the NUM-LOCK key

VB
AddHotKey("Scroll") 'for the SCROLL-LOCK key

VB
AddHotKey("F1") 'for the F1 Function key


You can hook practically every key in your Keyboard with this tool. Even the Function keys can be handled as it is able to perform the very base-level hooking of keys in your keyboard and therefore intercepts all the messages coming from your Keyboard first-hand. However, this can bring about issues whenever you've hooked the typing keys like Q or S, with the tool from your application. This is because you will now not be able to type-in any more with that key as the message that the key sends to any active application that requires input, say a Text editor, the message that the key sends will now be blocked from handing to the input what a user is typing. There can however be a way this can be solved, especially using function arguments (or function returns) that will return the default Message to the active application while performing the routines that you've added to the keys. Currently, I'm trying to work on this issue then will post the feedback here for the fix made (both for VB.NET and C# users). To the developer, great work and much appreciation for this handy job!

Hope this helps you and many others trying to use and/or find out the power of this great little tool for .NET apps!

modified 7-Aug-15 6:51am.

AnswerRe: Quick Help guide for this tool Pin
techbird10-Aug-15 2:34
techbird10-Aug-15 2:34 
GeneralRe: Quick Help guide for this tool Pin
Willy Kimura22-Aug-15 10:49
professionalWilly Kimura22-Aug-15 10:49 
Questionvery nice Pin
expert-programmer15-Nov-13 7:57
expert-programmer15-Nov-13 7:57 
Questionhow to use space and arrow key Pin
Hitesh Rohilla23-Jun-13 6:42
professionalHitesh Rohilla23-Jun-13 6:42 
QuestionCan't we ? Pin
Tarek Elqusi7-Jan-13 0:45
professionalTarek Elqusi7-Jan-13 0:45 
AnswerRe: Can't we ? Pin
SpriteFUN17-Mar-18 2:54
SpriteFUN17-Mar-18 2:54 
QuestionF12 is not Working Pin
luckyaruns3-Feb-12 3:49
luckyaruns3-Feb-12 3:49 
QuestionNOt able to register F12 Pin
luckyaruns24-Jan-12 2:45
luckyaruns24-Jan-12 2:45 
GeneralMy vote of 5 Pin
matadur18-Oct-10 9:05
matadur18-Oct-10 9:05 
GeneralNice Pin
L Viljoen11-Aug-10 22:41
professionalL Viljoen11-Aug-10 22:41 
QuestionWindows? Pin
Christ Kennedy10-Aug-10 11:24
Christ Kennedy10-Aug-10 11:24 
AnswerRe: Windows? Pin
techbird11-Aug-10 6:49
techbird11-Aug-10 6:49 
AnswerRe: Windows? Pin
L Viljoen11-Aug-10 22:43
professionalL Viljoen11-Aug-10 22:43 
AnswerRe: Windows? Pin
techbird12-Aug-10 20:55
techbird12-Aug-10 20:55 
GeneralRe: Windows? Pin
Christ Kennedy13-Aug-10 2:54
Christ Kennedy13-Aug-10 2:54 
GeneralRe: Windows? Pin
techbird17-Aug-10 6:48
techbird17-Aug-10 6:48 
AnswerRe: Windows? Pin
Willy Kimura7-Aug-15 0:05
professionalWilly Kimura7-Aug-15 0:05 
GeneralVery Useful Pin
ScruffyDuck10-Aug-10 10:20
ScruffyDuck10-Aug-10 10:20 
QuestionWhat's about global hot keys? Pin
tarasn9-Aug-10 21:15
tarasn9-Aug-10 21:15 
AnswerRe: What's about global hot keys? Pin
techbird10-Aug-10 8:03
techbird10-Aug-10 8:03 
GeneralRe: What's about global hot keys? Pin
tarasn10-Aug-10 10:53
tarasn10-Aug-10 10:53 

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.