Click here to Skip to main content
12,829,458 members (43,186 online)
Click here to Skip to main content
Add your own
alternative version


63 bookmarked
Posted 7 Aug 2006

A simple hotkey selection control for .NET

, 7 Aug 2006
Rate this:
Please Sign up or sign in to vote.
An article that shows off a simple hotkey selection control

Sample Image - hotkeycontrol.png


I'm writing an application that adds, among other things, global hotkey support to media players that don't natively support them, such as iTunes.

Microsoft's hotkey control ("msctls_hotkey32", or HOTKEY_CLASS) has a couple of limitations. For one, it recognizes the media buttons that many modern keyboards has as letters. That might not seem like such a big deal - but it is in some cases.

I wanted to make sure the user didn't select invalid hotkeys, such as Shift+<letter> (which isn't a good idea, since people use their keyboards to... well, type!). With Microsoft's control, this turned out to be pretty much impossible. You can tell the control to disallow such hotkeys, however, since it sees media keys as regular letters, it will force modifiers on media keys as well! What good is it to have a play/pause button on the keyboard if you need to hold down Ctrl+Alt to use it?

That's why I created this control from scratch, instead of basing it on Microsoft's control as I did in earlier versions of my program.

The sample application

I provided a sample application with this article, to make it easy for you to try the control without building your own project. The application itself is fairly useless, it lets you enter a hotkey and copy it to another HotkeyControl. Nevertheless, it demonstrates all the necessary functions.

Using the control in your project

First, as with all other custom controls, you need to add a reference to the assembly. To do so, right-click "References" in the Solution Explorer window, and pick "Add Reference...".

Go to the "Browse" tab, and double-click HotkeyControl.dll (in the location where you saved it).

To use it in the graphical Designer, you might need to add it to your Toolbox window as well. Right-click in an empty area in the Toolbox, and select "Choose Items...". Again, browse to HotkeyControl.dll, and make sure that "HotkeyControl" is checked in the list (use the Filter to narrow it down).

The control is very easy to use. After adding an instance of the HotkeyControl to your form (using the Windows Forms Designer, if you wish), you can read/set the controls' Hotkey and HotkeyModifiers properties to retrieve/set the hotkey.

private void btnSet_Click(object sender, EventArgs e)
    hotkeyControl.Hotkey = Keys.F11;
    hotkeyControl.HotkeyModifiers = Keys.Control | Keys.Alt;

private void btnShowHotkey_Click(object sender, EventArgs e)
    MessageBox.Show("Hotkey modifiers: " + hotkeyControl.HotkeyModifiers.ToString() 
        + Environment.NewLine + "Hotkey: " + hotkeyControl.Hotkey.ToString());


This control, just like Microsoft's control, only lets you select hotkeys. It does not provide a way to actually register and manage the hotkeys. You will have to code that part yourself. That shouldn't be too hard though, after looking up RegisterHotKey and UnregisterHotKey on P/Invoke.NET and in the MSDN Library.
If it is requested by many of you, I will look into writing such a class.


  • 7 Aug 2006 - Original release


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Sweden Sweden
No Biography provided

You may also be interested in...


Comments and Discussions

SuggestionReally usefull for me! Pin
Martin Solovey17-Apr-14 11:38
professionalMartin Solovey17-Apr-14 11:38 
GeneralMy vote of 5 Pin
vbfengshui22-Oct-11 17:43
membervbfengshui22-Oct-11 17:43 
Generalwe should convert modifiers when call RegisterHotKey Pin
TairGee22-Jan-11 22:45
memberTairGee22-Jan-11 22:45 
GeneralApply HotKey from text Pin
edgar_ferreira30-Nov-10 9:39
memberedgar_ferreira30-Nov-10 9:39 
QuestionUse KeysConverter to render the hotkey text? Pin
tonyblues3-Aug-09 22:47
membertonyblues3-Aug-09 22:47 
Generalbleh Pin
Beast6426-Jul-09 22:54
memberBeast6426-Jul-09 22:54 
GeneralMy vote of 1 Pin
Beast6426-Jul-09 22:52
memberBeast6426-Jul-09 22:52 
Question"Shift + Tab" doesn't work Pin
wing70prayer2-Sep-06 22:01
memberwing70prayer2-Sep-06 22:01 
GeneralWin Key Pin
jgallen2323-Aug-06 18:28
memberjgallen2323-Aug-06 18:28 
GeneralRe: Win Key Pin
Telrunya24-Aug-06 1:32
memberTelrunya24-Aug-06 1:32 
QuestionRe: Win Key Pin
BugByter23-Jun-07 9:25
memberBugByter23-Jun-07 9:25 
GeneralYou're right! Pin
fwsouthern7-Aug-06 18:26
memberfwsouthern7-Aug-06 18:26 
AnswerRe: You're right! Pin
Telrunya7-Aug-06 23:13
memberTelrunya7-Aug-06 23:13 
GeneralRe: You're right! Pin
fwsouthern8-Aug-06 2:45
memberfwsouthern8-Aug-06 2:45 
AnswerRe: You're right! Pin
Telrunya8-Aug-06 3:42
memberTelrunya8-Aug-06 3:42 
Lets see.
The first (#1) might do (close to) the same thing as my control, however, it isn't the same kind of control. It would look pretty weird with five such controls on the same form. Also, the two first limitations listed in the article are exactly why I created this control in the first place.

The second (#3) doesn't appear to be a control that lets the user actually select hotkeys; thus it's not the same either. Also, by a brief look at the source, it seems like adding multiple hotkeys would require severe modifications to the code.

The third (#6) doesn't provide any form of a wrapper, it's simply the same information you'd find on P/Invoke or MSDN, since it only calls the Win32 APIs directly.

The fourth (#12) actually seems to do something similar, however, again: it's not a user control, it's a popup killer... Meaning people would have to extract the code. Also, it doesn't seem to validate hotkeys at all.

The fifth (#13) "If you want to change the default hotkey, just set RuntimeObjectEditor.ObjectEditor.Instance.HotKey to whatever key combination you want." In other words, doesn't let the user select. Also, this is again not a control but rather a whole application.

The sixth (#17) "RegisterHotKey(Handle, 0x988B, 0x0002/*control*/, 0x7B/*F12*/)" Hotkey set in code, not a control.

The seventh (#19) contains the same hotkey system as one of the above projects (#3 I believe).

The eighth (#20) "In case you are only looking for a means to add hotkey support to your application, you might also want to take a look at the CodeProject article '.NET System wide hotkey component'."
A quick look at the component he suggests shows that it's not a control, merely code to register the hotkeys (which with some work could be combined with my control). It's no replacement, though, since the articles describe completely different things. Combined the two might work fine, but that doesn't mean that my control is suddenly a reinvention of the wheel.

And lastly, #31 only allows the usage of A-Z with modifier keys. And, of course, it's not a hotkey selection control, nor does it appear to contain one.

It feels like I just wasted 15 minutes. Do you still think I should remove this article? If so, why?

I didn't reinvent the wheel (HOTKEY_CLASS control), I just made it a bit better.
GeneralRe: You're right! Pin
fwsouthern8-Aug-06 8:33
memberfwsouthern8-Aug-06 8:33 
GeneralRe: You're right! Pin
gthimmes3-Dec-06 5:37
membergthimmes3-Dec-06 5:37 
GeneralRe: You're right! Pin
incrediclint5-Jun-12 9:23
memberincrediclint5-Jun-12 9:23 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170326.1 | Last Updated 7 Aug 2006
Article Copyright 2006 by Telrunya
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid