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

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.

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

About the Author

Telrunya

Sweden Sweden
No Biography provided

Comments and Discussions

 
Question"Shift + Tab" doesn't work Pinmemberwing70prayer2-Sep-06 21:01 

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 | Mobile
Web03 | 2.8.140709.1 | Last Updated 7 Aug 2006
Article Copyright 2006 by Telrunya
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid