close
最近接了新任務,所以有這部分的需求,
然後這部分我覺得應該是很有趣的地方,
所以就萊寫一篇教學文吧。
畢竟,hook可以用到的地方太多太多了,
接下來,我們就看下去吧。
Step1. 我們先在Global 那邊,放兩個變數
HWND g_CurrentWnd = NULL;
HHOOK myHook = NULL;
Step2.再加上這段Code
LRESULT CALLBACK KeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
char text[50], data[20];
const char *info = NULL;
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)lParam;
HDC hdc;
if (nCode >= 0)
{
if (wParam == WM_KEYDOWN) info = "WM_KEYDOWN";
else if (wParam == WM_KEYUP) info = "WM_KEYUP";
else if (wParam == WM_SYSKEYDOWN) info = "WM_SYKEYDOWN";
else if (wParam == WM_SYSKEYUP) info = "WM_SYSKEYUP";
//initialization array
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(g_CurrentWnd);
TextOut(hdc, 10, 10, text, strlen(text));
TextOut(hdc, 10, 30, data, strlen(data));
ReleaseDC(g_CurrentWnd, hdc);
}
return CallNextHookEx(myHook, nCode, wParam, lParam);
}
Step3.在main那邊新增
myHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyBoardProc, hInstance,0);
if (myHook == NULL)
{
return 0;
}
全站熱搜
留言列表