Click here to Skip to main content
Licence CPOL
First Posted 9 Aug 2010
Views 11,477
Downloads 424
Bookmarked 42 times

Smart Hotkey Handler .NET

By | 9 Aug 2010 | Article
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.
    this.hotKey1.HotKeyPressed += 
    	new SmartHotKey.HotKey.HotKeyEventHandler(hotKey1_HotKeyPressed); 
  4. Add the hot key press event handler.
    void hotKey1_HotKeyPressed(object sender, SmartHotKey.HotKeyEventArgs e)
    {
            MessageBox.Show(e.HotKey);
    } 
  5. Register all required hot-keys.
    //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.
    //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)

About the Author

techbird

Software Developer (Senior)
Cherrytec
India India

Member

Currently I am working in Cherrytec as a Senior Software developer.I work on Assembly Language(turbo assembler),C,C++,C#,Java,VB,VB.Net.I have experience on developing Desktop application,Asp.Net web application,windows services,web services,Mobile application(.Net CF),WPF,WCF,WWF and Sliver Light.Also I have working experience on workflow tools like K2.Net and Skelta BPM.Net.I love the art of software development and UML.Music is my destiny.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionF12 is not Working Pinmemberluckyaruns3:49 3 Feb '12  
QuestionNOt able to register F12 Pinmemberluckyaruns2:45 24 Jan '12  
GeneralMy vote of 5 Pinmemberoned29:05 18 Oct '10  
GeneralNice PinmemberChona117122:41 11 Aug '10  
QuestionWindows? PinmemberChrist Kennedy11:24 10 Aug '10  
AnswerRe: Windows? Pinmembertechbird6:49 11 Aug '10  
AnswerRe: Windows? PinmemberChona117122:43 11 Aug '10  
AnswerRe: Windows? Pinmembertechbird20:55 12 Aug '10  
GeneralRe: Windows? PinmemberChrist Kennedy2:54 13 Aug '10  
GeneralRe: Windows? Pinmembertechbird6:48 17 Aug '10  
GeneralVery Useful PinmemberScruffyDuck10:20 10 Aug '10  
QuestionWhat's about global hot keys? Pinmembertarasn21:15 9 Aug '10  
AnswerRe: What's about global hot keys? Pinmembertechbird8:03 10 Aug '10  
GeneralRe: What's about global hot keys? Pinmembertarasn10:53 10 Aug '10  
GeneralRe: What's about global hot keys? Pinmembertechbird6:51 11 Aug '10  
GeneralBetter as a Tip/Trick or Blog post Pinmemberdigital man21:26 8 Aug '10  
GeneralRe: Better as a Tip/Trick or Blog post Pinmembertechbird7:51 10 Aug '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web03 | 2.5.120515.1 | Last Updated 9 Aug 2010
Article Copyright 2010 by techbird
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid