How to use esc to stop a program in the middle of its execution in C?

2

I would like to stop the program by pressing ESC at any time, how could it be?

    
asked by anonymous 14.12.2017 / 11:09

1 answer

0

Using the getch function, you can get the character that was typed in the console, in which case the number 27 represents the keyboard ESC.

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

int main()
{
    char opcao;
    while(opcao != 27)
    {
        printf("\n\n\nEscolha uma opcao:\n");

        printf("<esc> para sair\n\n");

        opcao = getch();

        if (opcao != 27)
        {
            printf("Voce nao apertou ESC\n\n");
        }     
    }       
    return 0;
}
    
14.12.2017 / 11:21