环境:
我在 Win10 x64 [16299.431] 上使用 VS2010。
我想使用SetWindowsHookEx
功能在我的电脑上记录一些键。
最终目标:
如果有人按顺序输入正确的密码,我的电脑将自动解锁。
问题:它在权限
上不起作用。SYSTEM
执行:
#define LOCK_ENABLE 0
#define LOCK_DISABLE 1
DWORD g_locker = LOCK_DISABLE;
DWORD g_selwrd = -2;
CHAR* g_szpaswd = "heaven";
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
HOOKPROC LockerPraq();
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) lParam;
if(HC_ACTION == nCode)
{
switch (wParam)
{
case WM_KEYDOWN: case WM_SYSKEYDOWN:
switch (p->vkCode) {
case VK_HOME:
g_locker = LOCK_ENABLE;
g_selwrd = -1;
break;
case VK_END:
g_locker = LOCK_DISABLE;
g_selwrd = 0;
break;
}
if (LOCK_ENABLE == g_locker)
{
if (-1 == g_selwrd)
g_selwrd = 0;
else
{
CHAR szKbdstate[MAX_PATH] = {0};
WORD theChar = 0x0000;
ToAsciiEx(p->vkCode, p->scanCode, (BYTE*)szKbdstate, &theChar, 0, GetKeyboardLayout(0));
if (theChar != g_szpaswd[g_selwrd])
g_selwrd = 0;
else
{
g_selwrd++;
if (g_selwrd >= strlen(g_szpaswd))
UnLockPC();
}
}
}
break;
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
HOOKPROC LockerPraq()
{
HHOOK hook_install = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, NULL, 0);
if (NULL == hook_install)
return(0);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnhookWindowsHookEx(hook_install);
return(0);
}