键盘记录器应用程序使用诸如Win32 Hooks之类的机制。具体来说,您需要设置一个WH_KEYBOARD
钩子。
还有更高级的技术,比如创建自己的键盘驱动程序,但钩子是一个很好的选择。
编辑:为了让您了解钩子过程的样子,我在这里发布了我的个人实用程序的片段。
thehook = SetWindowsHookEx( WH_KEYBOARD_LL, hook_proc, hwnd, 0 );
LRESULT CALLBACK
hook_proc( int code, WPARAM wParam, LPARAM lParam )
{
static long ctrl_cnt = 0;
static bool mmode = false;
static DWORD time;
KBDLLHOOKSTRUCT* kbd = (KBDLLHOOKSTRUCT*)lParam;
if ( code < 0
|| (kbd->flags & 0x10)
) return CallNextHookEx( thehook, code, wParam, lParam );
long ret = 1;
if ( mmode ) {
if ( WM_KEYDOWN == wParam )
PostMessage(mainwnd, WM_MCR_ACCUM, kbd->vkCode, 0);
if ( WM_KEYUP == wParam )
switch (kbd->vkCode) {
case VK_ESCAPE:
mmode = false;
keys.removeall();
PostMessage(mainwnd, WM_MCR_HIDE, 0, 0);
break;
case VK_RETURN:
PostMessage(mainwnd, WM_MCR_EXEC, 0, 0);
break;
case VK_LCONTROL:
mmode = false;
PostMessage(mainwnd, WM_MCR_HIDE, 0, 0);
PostMessage(mainwnd, WM_MCR_EXEC, 0, 0);
break;
}
switch( kbd->vkCode ) {
case VK_LCONTROL:
case VK_CAPITAL:
case VK_LSHIFT:
case VK_RSHIFT:
ret = CallNextHookEx( thehook, code, wParam, lParam );
}
}
else {
if ( kbd->vkCode == VK_LCONTROL && WM_KEYDOWN == wParam ) {
ctrl_cnt = 1;
time = kbd->time;
}
if ( kbd->vkCode != VK_LCONTROL )
ctrl_cnt = 0;
if ( ctrl_cnt == 1 && WM_KEYUP == wParam ) {
if ( kbd->time - time > 40 ) {
mmode = true;
PostMessage(mainwnd, WM_MCR_SHOW, 0, 0);
}
}
ret = CallNextHookEx( thehook, code, wParam, lParam );
}
return ret;
}