Click here to Skip to main content
13,559,672 members
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 10:38
professionalMartin Solovey17-Apr-14 10:38 
GeneralMy vote of 5 Pin
vbfengshui22-Oct-11 16:43
membervbfengshui22-Oct-11 16:43 
Generalwe should convert modifiers when call RegisterHotKey Pin
TairGee22-Jan-11 21:45
memberTairGee22-Jan-11 21:45 
GeneralApply HotKey from text Pin
edgar_ferreira30-Nov-10 8:39
memberedgar_ferreira30-Nov-10 8:39 
QuestionUse KeysConverter to render the hotkey text? Pin
tonyblues3-Aug-09 21:47
membertonyblues3-Aug-09 21:47 
Generalbleh Pin
Beast6426-Jul-09 21:54
memberBeast6426-Jul-09 21:54 
GeneralMy vote of 1 Pin
Beast6426-Jul-09 21:52
memberBeast6426-Jul-09 21:52 
Question"Shift + Tab" doesn't work Pin
wing70prayer2-Sep-06 21:01
memberwing70prayer2-Sep-06 21:01 
GeneralWin Key Pin
jgallen2323-Aug-06 17:28
memberjgallen2323-Aug-06 17:28 
GeneralRe: Win Key Pin
Telrunya24-Aug-06 0:32
memberTelrunya24-Aug-06 0:32 
QuestionRe: Win Key Pin
BugByter23-Jun-07 8:25
memberBugByter23-Jun-07 8:25 
GeneralYou're right! Pin
fwsouthern7-Aug-06 17:26
memberfwsouthern7-Aug-06 17:26 
AnswerRe: You're right! Pin
Telrunya7-Aug-06 22:13
memberTelrunya7-Aug-06 22:13 
GeneralRe: You're right! Pin
fwsouthern8-Aug-06 1:45
memberfwsouthern8-Aug-06 1:45 
AnswerRe: You're right! Pin
Telrunya8-Aug-06 2:42
memberTelrunya8-Aug-06 2:42 
GeneralRe: You're right! Pin
fwsouthern8-Aug-06 7:33
memberfwsouthern8-Aug-06 7:33 
GeneralRe: You're right! Pin
gthimmes3-Dec-06 4:37
membergthimmes3-Dec-06 4:37 
GeneralRe: You're right! Pin
incrediclint5-Jun-12 8:23
memberincrediclint5-Jun-12 8: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 | Cookies | Terms of Use | Mobile
Web02-2016 | 2.8.180527.1 | Last Updated 7 Aug 2006
Article Copyright 2006 by Telrunya
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid