BlackWaspTM
Input / Output
.NET 1.1+

Registering System-Wide Hot Keys (2)

Some Windows applications execute in the background and are activated only as required. To enhance such a program's usability it is useful to register a system-wide hot key. This allows the software to activate when a specific key combination is pressed.

Hot Key API Constants

When calling the RegisterHotKey function, the fsModifiers parameter is used to specify which keys must be used in conjunction with the hot key to trigger a message. In the rare instance that the hot key is to be used in isolation, you should pass zero to this parameter. In all other cases, you should pass a value that represents the combination of the four possible modifier keys that you wish to register.

To improve the readability and maintainability of the code, we will declare four constants within the form's class to represent the four modifier keys. To use more than one of the modifier keys, the appropriate constants can be combined using the bitwise OR operator.

To declare the four constants, add the code below to the form's class. This includes a fifth constant named "WM_HOTKEY". This constant holds a value that represents the type of message that is sent when a hot key is pressed. We will use this value when processing incoming messages to filter out other message types.

const int MOD_ALT = 0x1;
const int MOD_CONTROL = 0x2;
const int MOD_SHIFT = 0x4;
const int MOD_WIN = 0x8;
const int WM_HOTKEY = 0x312;

Registering the Hot Key

Now that we have added the Windows API functions and associated constants, we can register a hot key during the Load event of our form. We will register the keyboard combination of the Windows key and Q, so that whenever these two keys are pressed simultaneously a message will be sent to the form. We will add the code to react to the message later in the article.

To register the hot key, use the form designer to add the Load event to the form and then modify the event's code as shown below:

private void Form1_Load(object sender, EventArgs e)
{
    MessageBox.Show(RegisterHotKey((IntPtr)Handle, 1, MOD_WIN, (int)Keys.Q).ToString());
}

You can see that the first parameter uses the handle of the form, cast as IntPtr, to specify the window that will receive the message. The unique ID for the registered key is 1. The last two parameters specify that the Windows key will be the modifier and that Q is the hot key. If this key combination is already in use on your system, or if you do not have a Windows key, modify these parameters to select different options. Note also that the return value of the function is being converted to a string and displayed in a message box. This will allow you to see whether the registration is successful or not. In production code you should obtain the return value and react appropriately to a registration failure.

Unregistering the Hot Key

It is important that any registered hot keys are unregistered when they are no longer required. For the sample, we will unregister the hot key in the FormClosing event. Add this event to the project and include the following code:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    MessageBox.Show(UnregisterHotKey((IntPtr)Handle, 1).ToString());
}
14 June 2009