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

BOOL WINAPI ConsoleCtrlHandler(DWORD ctrlType) {
    if (ctrlType == CTRL_C_EVENT) {
        printf("\n^C erkannt (CTRL_C_EVENT)\n");
        return TRUE;
    }
    return FALSE;
}

int main() {
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    DWORD mode = 0;
    SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);

    GetConsoleMode(hOut, &mode);
    mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
    SetConsoleMode(hOut, mode);

    printf("\x1b[2J\x1b[H");
    printf("\x1b[1;36mESC-Sequenz-Tester (_getch Version)\x1b[0m\n\n");

    while (1) {
        int c1 = _getch();
        if (c1 == 27) {
            if (_kbhit()) {
                int c2 = _getch();
                printf("ESC-%d (zweites Byte)\n", c2);
            } else {
                printf("ESC gedrueckt → Programmende\n");
                break;
            }
        } else if (c1 == 3) {
            printf("^C gedrueckt → continue\n");
        } else if (c1 >= 32 && c1 <= 126) {
            printf("Taste: '%c' (%d)\n", c1, c1);
        } else {
            printf("Steuerzeichen: Code %d\n", c1);
        }
    }
    return 0;
}