Click here to Skip to main content
15,881,803 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 
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 
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 7:33
fwsouthern8-Aug-06 7:33 
GeneralRe: You're right! Pin
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.