Retrieving and Setting a Hot Key
After the user has chosen a hot key, an application should retrieve the hot key from the hot-key control by using the HKM_GETHOTKEY message. This message retrieves a 16-bit value that contains the virtual-key code and modifier keys describing the hot key.
The following function retrieves a key combination from a hot-key control and then uses the WM_SETHOTKEY message to set a global hot key. Note that you cannot set a global hot key for a window that has the WS_CHILD window style.
// ProcessHotkey - retrieves the hot key from the hot-key control and
// sets it as the hot key for the application's main window.
// Returns TRUE if successful or FALSE otherwise.
// hwndHot - handle of the hot-key control
// hwndMain - handle of the main window
BOOL WINAPI ProcessHotkey(HWND hwndHot, HWND hwndMain)
{
WORD wHotkey;
UINT iSetResult;
// Retrieve the hot key (virtual-key code and modifiers).
wHotkey = SendMessage(hwndHot, HKM_GETHOTKEY, 0, 0);
// Use the result as wParam for WM_SETHOTKEY.
iSetResult = SendMessage(hwndMain, WM_SETHOTKEY, wHotkey, 0);
switch (iSetResult) {
case 2: // WM_SETHOTKEY succeeded
MessageBox(NULL, "Hot key previously assigned",
"Okay", MB_OK);
return TRUE;
case 1: // WM_SETHOTKEY succeeded
return TRUE;
case 0:
MessageBox(NULL, "Invalid window for hot key",
"Error", MB_OK);
return FALSE;
case -1:
MessageBox(NULL, "Invalid hot key",
"Error", MB_OK);
return FALSE;
default:
MessageBox(NULL, "Unknown error", "Error", MB_OK);
return FALSE;
}
}