Click here to Skip to main content
Click here to Skip to main content

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

Comments and Discussions

SuggestionReally usefull for me! PinprofessionalMartin Solovey17-Apr-14 11:38 
GeneralMy vote of 5 Pinmembervbfengshui22-Oct-11 17:43 
Generalwe should convert modifiers when call RegisterHotKey PinmemberTairGee22-Jan-11 22:45 
Here is sample for your reference.
fromModifiers = (int)HotKeyControl.HotkeyModifiers;
        const int HOTKEYF_SHIFT = 0x01;
        const int HOTKEYF_CONTROL = 0x02;
        const int HOTKEYF_ALT = 0x04;
        const int HOTKEYF_EXT = 0x08;
        const int MOD_ALT = 0x1;
        const int MOD_CONTROL = 0x2;
        const int MOD_SHIFT = 0x4;
        const int MOD_WIN = 0x8;
        public int RegisterHotKey(int Key, int fromModifiers, string idString)
            // Convert Modifiers to fit HKM_SETHOTKEY
            // the Modifiers is diffrent between WM_SETHOTKEY and HKM_SETHOTKEY
            fromModifiers = fromModifiers >> 16;
            int toModifiers = 0;
            if ((fromModifiers & HOTKEYF_ALT) > 0)
                toModifiers |= MOD_ALT;
            if ((fromModifiers & HOTKEYF_CONTROL) > 0)
                toModifiers |= MOD_CONTROL;
            if ((fromModifiers & HOTKEYF_EXT) > 0)
                toModifiers |= MOD_WIN;
            if ((fromModifiers & HOTKEYF_SHIFT) > 0)
                toModifiers |= MOD_SHIFT;
            UInt32 hotkeyid = GlobalAddAtom(System.Guid.NewGuid().ToString());
            RegisterHotKey((IntPtr)hWnd, hotkeyid, (UInt32)toModifiers, (UInt32)Key);
            keyIDs[hotkeyid] = idString;
            return (int)hotkeyid;
This is a great .net control, thanks Telrunya
GeneralApply HotKey from text Pinmemberedgar_ferreira30-Nov-10 9:39 
QuestionUse KeysConverter to render the hotkey text? Pinmembertonyblues3-Aug-09 22:47 
Generalbleh PinmemberBeast6426-Jul-09 22:54 
GeneralMy vote of 1 PinmemberBeast6426-Jul-09 22:52 
Question"Shift + Tab" doesn't work Pinmemberwing70prayer2-Sep-06 22:01 
GeneralWin Key Pinmemberjgallen2323-Aug-06 18:28 
GeneralRe: Win Key PinmemberTelrunya24-Aug-06 1:32 
QuestionRe: Win Key PinmemberBugByter23-Jun-07 9:25 
GeneralYou're right! Pinmemberfwsouthern7-Aug-06 18:26 
AnswerRe: You're right! PinmemberTelrunya7-Aug-06 23:13 
GeneralRe: You're right! Pinmemberfwsouthern8-Aug-06 2:45 
AnswerRe: You're right! PinmemberTelrunya8-Aug-06 3:42 
GeneralRe: You're right! Pinmemberfwsouthern8-Aug-06 8:33 
GeneralRe: You're right! Pinmembergthimmes3-Dec-06 5:37 
GeneralRe: You're right! Pinmemberincrediclint5-Jun-12 9:23 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150327.1 | Last Updated 7 Aug 2006
Article Copyright 2006 by Telrunya
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid