Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This topic demonstrates how to create a hot key control. You create a hot key control by using the CreateWindowEx function, specifying the HOTKEY_CLASS window class.
What you need to know
Technologies
Prerequisites
- C/C++
- Windows User Interface Programming
Instructions
Before you create the hot key control, ensure that the common controls DLL is loaded.
In the following C++ code example, the application-defined function calls the InitCommonControlsEx function to load the common control DLL. Then it calls the CreateWindowEx function, specifying the HOTKEY_CLASS window class, to create a hot key control. It uses the HKM_SETRULES and HKM_SETHOTKEY messages to initialize the control and returns a handle to the control.
This hot key control does not allow the user to choose a hot key that is a single unmodified key, nor does it permit the user to choose only SHIFT and a key. These rules effectively prevent the user from choosing a hot key that might be entered accidentally while typing text.
// Creates a hot key control and sets rules and default settings for it.
//
// Returns the handle of the hot key control.
//
// Parameter
// hwndDlg - Handle of the parent window (dialog box).
//
// Global variable
// g_hinst - Handle of the application instance.
extern HINSTANCE g_hinst;
HWND WINAPI InitializeHotkey(HWND hwndDlg)
{
HWND hwndHot = NULL;
// Ensure that the common control DLL is loaded.
INITCOMMONCONTROLSEX icex; //declare an INITCOMMONCONTROLSEX Structure
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_HOTKEY_CLASS; //set dwICC member to ICC_HOTKEY_CLASS
// this loads the Hot Key control class.
InitCommonControlsEx(&icex);
hwndHot = CreateWindowEx(0, // no extended styles
HOTKEY_CLASS, // class name
TEXT(""), // no title (caption)
WS_CHILD | WS_VISIBLE, // style
15, 10, // position
200, 20, // size
hwndDlg, // parent window
NULL, // uses class menu
g_hinst, // instance
NULL); // no WM_CREATE parameter
SetFocus(hwndHot);
// Set rules for invalid key combinations. If the user does not supply a
// modifier key, use ALT as a modifier. If the user supplies SHIFT as a
// modifier key, use SHIFT + ALT instead.
SendMessage(hwndHot,
HKM_SETRULES,
(WPARAM) HKCOMB_NONE | HKCOMB_S, // invalid key combinations
MAKELPARAM(HOTKEYF_ALT, 0)); // add ALT to invalid entries
// Set CTRL + ALT + A as the default hot key for this window.
// 0x41 is the virtual key code for 'A'.
SendMessage(hwndHot,
HKM_SETHOTKEY,
MAKEWORD(0x41, HOTKEYF_CONTROL | HOTKEYF_ALT),
0);
return hwndHot;
}
Related topics