Apparently, it can't be a console app...this code seems to work fine (I couldn't get it to work as a console app, although I could get it to work as a console app when I tested the left mouse button):
Code:
#include <windows.h>
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT uiMsg,
WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR pszCmdLine, int nShowCmd)
{
MSG msg;
WNDCLASS wndclass;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = MainWndProc;
wndclass.lpszClassName = "KeyStateTestClass";
wndclass.lpszMenuName = NULL;
wndclass.style = 0;
RegisterClass(&wndclass);
HWND hwnd = CreateWindow("KeyStateTestClass", "Key Test",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT uiMsg,
WPARAM wParam, LPARAM lParam)
{
switch(uiMsg)
{
case WM_CREATE:
SetTimer(hwnd, 1, 0, NULL);
return 0;
case WM_DESTROY:
KillTimer(hwnd, 1);
PostQuitMessage(0);
return 0;
case WM_TIMER:
if (GetAsyncKeyState(VK_F2) & 0x8000)
{
MessageBox(hwnd, "Got F2", "Test", MB_OK);
}
return 0;
}
return ::DefWindowProc(hwnd, uiMsg, wParam, lParam);
}