#include <windows.h>#include <commctrl.h> #pragma comment(lib,"comctl32.lib") #define IDC_HOTKEY 1001 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, int nCmdShow) { INITCOMMONCONTROLSEX icc; icc.dwSize = sizeof(icc); icc.dwICC = ICC_WIN95_CLASSES; InitCommonControlsEx(&icc); WNDCLASS wc = {0}; wc.lpfnWndProc = WndProc; wc.hInstance = hInst; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1); wc.lpszClassName = TEXT("HotKeyExample"); RegisterClass(&wc); HWND hwnd = CreateWindow( TEXT("HotKeyExample"), TEXT("Windows API Hot Key Control"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 400, 180, NULL, NULL, hInst, NULL); ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static HWND hHotKey; switch(msg) { case WM_CREATE: CreateWindow( TEXT("STATIC"), TEXT("Press a shortcut:"), WS_CHILD | WS_VISIBLE, 20, 20, 120, 20, hwnd, NULL, GetModuleHandle(NULL), NULL); hHotKey = CreateWindowEx( 0, HOTKEY_CLASS, NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 20, 45, 200, 25, hwnd, (HMENU)IDC_HOTKEY, GetModuleHandle(NULL), NULL); // Default hot key = Ctrl+Alt+F1 SendMessage( hHotKey, HKM_SETHOTKEY, MAKEWORD(VK_F1, HOTKEYF_CONTROL | HOTKEYF_ALT), 0); return 0; case WM_COMMAND: if (LOWORD(wParam) == IDC_HOTKEY && HIWORD(wParam) == EN_CHANGE) { WORD hotkey = (WORD)SendMessage( hHotKey, HKM_GETHOTKEY, 0, 0); BYTE vk = LOBYTE(hotkey); BYTE mods = HIBYTE(hotkey); TCHAR text[100]; wsprintf( text, TEXT("Virtual Key = %u\nModifiers = %u"), vk, mods); MessageBox(hwnd, text, TEXT("Hot Key"), MB_OK); } return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, msg, wParam, lParam); }