Check the program.

I want to write a simple keylogger. Please, note that it's not a malware. The function of it only one - to save pressed buttons on keyboard to file "log.txt", which is not hidden, but located on the desktop. As you can see is should be pretty simple. It mustn't consist any stealth functions. Please look at this code, and say if it's good or not, I don't understand why it doesn't work, I compiled it in Dev C++, only exe and txt file created, but when I type keyboard nothing happends. log.txt is empty. Why it doesn't work?

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
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>

int main(void)
{
	std::ofstream File;
	File.open("log.txt");
	if( !File.is_open() )
		return EXIT_FAILURE;	// Error opening file.

	std::string log = "";
	std::string letter = "";

	int num = 0;
	int nTimer = clock();
	BOOL bUpdate = TRUE;

	while( !GetAsyncKeyState(VK_ESCAPE) )
	{
		Sleep(10);

		for( num = 32; num <= 90; num++ ) {
			switch(num)
			{
			case 65: letter = "a"; break;
			case 66: letter = "b"; break;
			case 67: letter = "c"; break;
			case 68: letter = "d"; break;
			case 69: letter = "e"; break;
			case 70: letter = "f"; break;
			case 71: letter = "g"; break;
			case 72: letter = "h"; break;
			case 73: letter = "i"; break;
			case 74: letter = "j"; break;
			case 75: letter = "k"; break;
			case 76: letter = "l"; break;
			case 77: letter = "m"; break;
			case 78: letter = "n"; break;
			case 79: letter = "o"; break;
			case 80: letter = "p"; break;
			case 81: letter = "q"; break;
			case 82: letter = "r"; break;
			case 83: letter = "s"; break;
			case 84: letter = "t"; break;
			case 85: letter = "u"; break;
			case 86: letter = "v"; break;
			case 87: letter = "w"; break;
			case 88: letter = "x"; break;
			case 89: letter = "y"; break;
			case 90: letter = "z"; break;
			case 32: letter = " "; break;
			}
			
			if( GetAsyncKeyState(num) )
			{
				if( clock() - nTimer > 200 || !bUpdate )
				{
					bUpdate = FALSE;
					nTimer = clock();
					
					File << letter;

					bUpdate = TRUE;
				}
			}
		}
	}

	File.close();

    return EXIT_SUCCESS;
}[code]
Last edited on
Please who can help me to check and correct this code? When it works the buttons are captured not correctly. For example, I write "hello there", and in the log file I see "hheelllloo tthheerree" or "hlo th". Which part of the code works not correctly?
Sorry this might not be of much help, but i think you should post this on the windows programming forum instead.
Here is a similar post from a while back that might help.
http://www.cplusplus.com/forum/lounge/27569/#msg147569

What I'd recommend is to retrieve the characters directly from the keyboard buffer, store them in a std::stringstream, then check the stream for letters and write those in the file.

Here's more about the windows keyboard buffer:
https://msdn.microsoft.com/en-us/library/windows/desktop/ee416238(v=vs.85).aspx
Last edited on
Topic archived. No new replies allowed.