Click here to Skip to main content
15,880,796 members
Articles / Programming Languages / C#
Article

A simple hotkey selection control for .NET

Rate me:
Please Sign up or sign in to vote.
4.50/5 (15 votes)
7 Aug 20062 min read 123.9K   3.3K   64   19
An article that shows off a simple hotkey selection control

Sample Image - hotkeycontrol.png

Introduction

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.

C#
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());
}

Notes

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.

History

  • 7 Aug 2006 - Original release

License

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


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

Comments and Discussions

 
QuestionAwesome! Pin
Willy Kimura15-Sep-19 18:48
professionalWilly Kimura15-Sep-19 18:48 
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
vbfengshui22-Oct-11 16:43 
Generalwe should convert modifiers when call RegisterHotKey Pin
TairGee22-Jan-11 21:45
TairGee22-Jan-11 21:45 
GeneralApply HotKey from text Pin
edgar_ferreira30-Nov-10 8:39
edgar_ferreira30-Nov-10 8:39 
QuestionUse KeysConverter to render the hotkey text? Pin
tonyblues3-Aug-09 21:47
tonyblues3-Aug-09 21:47 
Generalbleh Pin
Grumpet26-Jul-09 21:54
Grumpet26-Jul-09 21:54 
GeneralMy vote of 1 Pin
Grumpet26-Jul-09 21:52
Grumpet26-Jul-09 21:52 
If doesn't actually register the hotkey then tell me again how can this be useful?!
Question"Shift + Tab" doesn't work Pin
wing70prayer2-Sep-06 21:01
wing70prayer2-Sep-06 21:01 
GeneralWin Key Pin
jgallen2323-Aug-06 17:28
jgallen2323-Aug-06 17:28 
GeneralRe: Win Key Pin
Telrunya24-Aug-06 0:32
Telrunya24-Aug-06 0:32 
QuestionRe: Win Key Pin
BugByter23-Jun-07 8:25
BugByter23-Jun-07 8:25 
GeneralYou're right! Pin
fwsouthern7-Aug-06 17:26
fwsouthern7-Aug-06 17:26 
AnswerRe: You're right! Pin
Telrunya7-Aug-06 22:13
Telrunya7-Aug-06 22:13 
GeneralRe: You're right! Pin
fwsouthern8-Aug-06 1:45
fwsouthern8-Aug-06 1:45 
AnswerRe: You're right! Pin
Telrunya8-Aug-06 2:42
Telrunya8-Aug-06 2:42 
GeneralRe: You're right! Pin
fwsouthern8-Aug-06 7:33
fwsouthern8-Aug-06 7:33 
GeneralRe: You're right! PinPopular
gthimmes3-Dec-06 4:37
gthimmes3-Dec-06 4:37 
GeneralRe: You're right! Pin
incrediclint5-Jun-12 8:23
incrediclint5-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.