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 ("
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 "
" 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'
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
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