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

int main() {
    HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    DWORD inMode = 0, outMode = 0;

    GetConsoleMode(hIn, &inMode);
    SetConsoleMode(hIn, inMode | ENABLE_VIRTUAL_TERMINAL_INPUT);

    GetConsoleMode(hOut, &outMode);
    SetConsoleMode(hOut, outMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);

    printf("\x1b[2J\x1b[H");
    printf("\x1b[1;36mESC-Sequenz-Tester (ReadFile + VT-Input)\x1b[0m\n\n");
    printf("Druecken Sie Tasten (ESC beendet)...\n");

    char buf[16];
    DWORD read;
    while (1) {
        if (!ReadFile(hIn, buf, sizeof(buf), &read, NULL)) break;
        for (DWORD i = 0; i < read; ++i) {
            unsigned char ch = buf[i];
            printf("Byte[%lu]: 0x%02X '%c'\n", i, ch, (ch >= 32 && ch < 127) ? ch : '.');
            if (ch == 27 && read == 1) {
                printf("ESC erkannt → Programmende\n");
                return 0;
            }
        }
    }
    return 0;
}