#include <windows.h>
HWND hgWnd;
HHOOK myhook;
/****************************************************************
WH_KEYBOARD hook procedure
鍵盤钩子处理过程
****************************************************************/
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)lParam;
const char *info = NULL;
char text[50], data[20];
PAINTSTRUCT ps;
HDC hdc;
if (nCode >= 0)
{
if (wParam == WM_KEYDOWN) info = "普通按鍵抬起";
else if (wParam == WM_KEYUP) info = "普通按鍵按下";
else if (wParam == WM_SYSKEYDOWN) info = "系統按鍵抬起";
else if (wParam == WM_SYSKEYUP) info = "系統按鍵按下";
ZeroMemory(text, sizeof(text));
ZeroMemory(data, sizeof(data));
wsprintf(text, "%s - 键盘码 [%04d], 扫描码 [%04d] ", info, p->vkCode, p->scanCode);
wsprintf(data, "按鍵目測為: %c ", p->vkCode);
hdc = GetDC(hgWnd);
TextOut(hdc, 10, 10, text, strlen(text));
TextOut(hdc, 10, 30, data, strlen(data));
ReleaseDC(hgWnd,hdc);
}
return CallNextHookEx(myhook, nCode, wParam, lParam);
}
// 5. 窗口过程处理
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
hgWnd = hwnd;
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc; //
HWND hwnd;
MSG Msg;
char text[30];
const char szClassName[] = "myWindowClass";
// 1. 设置注册窗口结构体
wc.cbSize = sizeof(WNDCLASSEX); // 注册窗口结构体的大小
wc.style = 0; // 窗口的样式
wc.lpfnWndProc = WndProc; // 指向窗口处理过程的函数指针
wc.cbClsExtra = 0; // 指定紧跟在窗口类结构后的附加字节数
wc.cbWndExtra = 0; // 指定紧跟在窗口事例后的附加字节数