Virtual key press test

Pages: 12
Apr 12, 2009 at 7:01am
I searched for this and can't seem to find it...What is the code that's used for a key press by a user? I tried this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
string mystring;
    cin>>mystring;
    if (GetAsyncKeyState(VK_N))
    {
       cout<<" ";
    }
system("pause");
return0;
}

Just to see if I could get it to do a space when I pressed "n" but it said that there is an error in #include <windows.h>
Help?
Apr 12, 2009 at 7:10am
What does the error say?
(Is possible that you don't have windows.h)
Apr 12, 2009 at 7:16am
It says "No such file or directory"
Apr 12, 2009 at 7:19am
VK_N is no virtual key. N is not a virtual key, virtual keys are those keys which dont output anything when pressed.

do something like this:
istate = GetKeyState(VK_SHIFT); //to check for shift key
in this case the high bit will be set to signify shift key down and hence the value will be negative.

to check for characters do like this:
1
2
3
4
5
6
7
8
9
case WM_CHAR:
{
switch(wParam)
{
case 'n':
cout << "n key pressed";
break;
}
}

to use all this you need to create a win32 application as you can generate the events in console application.
Apr 12, 2009 at 7:22am
Apr 12, 2009 at 7:22am
Okay, thank you!
Apr 12, 2009 at 7:23am
You don't have Windows API or you don't have the right settings if you get that error.
Which compiler and IDE (if any) are you using?
On which operating system are you working?
Last edited on Apr 12, 2009 at 7:23am
Apr 12, 2009 at 8:33am
I'm running on windows XP and im using Dev-C++
Apr 12, 2009 at 8:35am
Okay, I'm attempting a keylogger and my idea is to have it use this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{ 
    string keys;
    int x = 1;
    cin >> keys;
    ofstream a_file ( "log.txt", ios::app );
    getline (cin, keys);
    a_file << keys << "\n";
    
    system("pause");
}

but have the program open itself back up when its done so that it will keep logging if the user presses enter...don't even know if that's possible. How do you run another program by the way?

Last edited on Apr 12, 2009 at 8:36am
Apr 12, 2009 at 8:38am
Sorry if I am annoying with my questions, I just really want to learn all this stuff! And much thanks Bazzy! You've been answering all my questions, you're awesome!
Last edited on Apr 12, 2009 at 8:40am
Apr 12, 2009 at 8:46am
My other idea for a keylogger is this one:
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
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{ 
    char key;
    int x = 1;
    cin >> key;
    ofstream a_file ( "log.txt", ios::app );
    switch (key)
    {
           case 'A':
                a_file << "A"; 
                break;
           case 'B':
                a_file << "B";
                break;
           case 'C':
                a_file << "C";
                break;
           case 'D':
                a_file << "D";
                break;
           case 'E':
                a_file << "E";
                break;
           case 'F':
                a_file << "F";
                break;
           case 'G':
                a_file << "G";
                break;
           case 'H':
                a_file << "H";
                break;
           case 'I':
                a_file << "I";
                break;
           case 'J':
                a_file << "J";
                break;
           case 'K':
                a_file << "K";
                break;
           case 'L':
                a_file << "L";
                break;
           case 'M':
                a_file << "M";
                break;
           case 'N':
                a_file << "N";
                break;
           case 'O':
                a_file << "O";
                break;
           case 'P':
                a_file << "P";
                break;
           case 'Q':
                a_file << "Q";
                break;
           case 'R':
                a_file << "R";
                break;
           case 'S':
                a_file << "S";
                break;
           case 'T':
                a_file << "T";
                break;
           case 'U':
                a_file << "U";
                break;
           case 'V':
                a_file << "V";
                break;
           case 'W':
                a_file << "W";
                break;
           case 'X':
                a_file << "X";
                break;
           case 'Y':
                a_file << "Y";
                break;
           case 'Z':
                a_file << "Z";
                break;
           case 'a':
                a_file << "a";
                break;
           case 'b':
                a_file << "b";
                break;
           case 'c':
                a_file << "c";
                break;
           case 'd':
                a_file << "d";
                break;
           case 'e':
                a_file << "e";
                break;
           case 'f':
                a_file << "f";
                break;
           case 'g':
                a_file << "g";
                break;
           case 'h':
                a_file << "h";
                break;
           case 'i':
                a_file << "i";
                break;
           case 'j':
                a_file << "j";
                break;
           case 'k':
                a_file << "k";
                break;
           case 'l':
                a_file << "l";
                break;
           case 'm':
                a_file << "m";
                break;
           case 'n':
                a_file << "n";
                break;
           case 'o':
                a_file << "o";
                break;
           case 'p':
                a_file << "p";
                break;
           case 'q':
                a_file << "q";
                break;
           case 'r':
                a_file << "r";
                break;
           case 's':
                a_file << "s";
                break;
           case 't':
                a_file << "t";
                break;
           case 'u':
                a_file << "u";
                break;
           case 'v':
                a_file << "v";
                break;
           case 'w':
                a_file << "w";
                break;
           case 'x':
                a_file << "x";
                break;
           case 'y':
                a_file << "y";
                break;
           case 'z':
                a_file << "z";
                break;
           case ' ':
                a_file << " ";
                break;
           case '0':
                a_file << "0";
                break;
           case '1':
                a_file << "1";
                break;
           case '2':
                a_file << "2";
                break;
           case '3':
                a_file << "3";
                break;
           case '4':
                a_file << "4";
                break;
           case '5':
                a_file << "5";
                break;
           case '6':
                a_file << "6";
                break;
           case '7':
                a_file << "7";
                break;
           case '8':
                a_file << "8";
                break;
           case '9':
                a_file << "9";
                break;
           case '!':
                a_file << "!";
                break;
           case '@':
                a_file << "@";
                break;
           case '#':
                a_file << "#";
                break;
           case '$':
                a_file << "$";
                break;
           case '%':
                a_file << "%";
                break;
           case '^':
                a_file << "^";
                break;
           case '&':
                a_file << "&";
                break;
           case '*':
                a_file << "*";
                break;
           case '(':
                a_file << "{";
                break;
           case ')':
                a_file << "}";
                break;
           case '-':
                a_file << "-";
                break;
           case '_':
                a_file << "_";
                break;
           case '=':
                a_file << "=";
                break;
           case '+':
                a_file << "+";
                break;
           case ',':
                a_file << ",";
                break;
           case ':':
                a_file << ":";
                break;
           case '?':
                a_file << "?";
                break;
           case '.':
                a_file << ".";
                break;
           case '<':
                a_file << "<";
                break;
           case '>':
                a_file << ">";
                break;
           case '/':
                a_file << "/";
                break;
           case '[':
                a_file << "[";
                break;
           case ']':
                a_file << "]";
                break;
           case ';':
                a_file << ";";
                break;
           case '|':
                a_file << "|";
                break;
    }
    
    
    system("pause");
    return 0;
}

I just can't think of a loop or some other way to restart my swtich statement to accept more characters that the user types :/ because right now, it takes the first letter you type and inputs it to log.txt. Which brings up the issue of having the program type "Enter" after each key the user types to enter it into the file or something like that. This is why I was asking about virtual keys. But would enter after every key cause it to only put one letter anyway since the program would terminate after enter being pressed?
Last edited on Apr 12, 2009 at 8:51am
Apr 12, 2009 at 1:22pm
you cant write a key logger this way.. keyloggers are something which logs keys of the operating system. your program can only log keys of your own application. And secondly, your above code can only log character messages, you cant log virtual keys, dead keys etc etc..
on windows catching a new line is tricky as its made of \r\n as compared to linux\unix which has only \n for new line.

if you really want to write a key logger you can read about windows hook api's, thats the only way for keylogging.
Apr 12, 2009 at 7:33pm
Okay...thank you! This is going to be much more difficult than I thought!
Apr 13, 2009 at 10:33am
its not difficult.. i can help you if you want.
Apr 14, 2009 at 4:23am
That would be Awesome!
Apr 14, 2009 at 4:29am
tell me your yahoo id, will send you the binary first. look at it, if thats what you want then will post the code.

yahoo allows binary files to be mailed thats why, gmail blocks them.
Apr 14, 2009 at 5:31am
you can send it to fifty_patoots@yahoo.com please :) code please too
Apr 14, 2009 at 6:53am
send. compile it and enjoy..
code is not very big.. around 120 lines.. :D

compile the .dll and the .exe file. when you run the .exe the .dll should be in the same folder.
it will create the key logging file in the temp folder. if you are mashed then the file will be:

c:\\document and settings\mashed\local settings\temp\winbat.sam.


i hope you know some basics of dll's and win32 api's. else read them from some book.

enjoy.
Apr 14, 2009 at 6:56am
the mail failed. the yahoo a/c doesnt exist. tell me the correct one.
Apr 14, 2009 at 7:33am
wtf? I just tried to sign in and it said it didn't exists so I tried again and it let me in :/ would you be willing to try again? Thank you so much for the help btw! fifty_patoots@yahoo.com
Pages: 12