I would like to stop the program by pressing ESC at any time, how could it be?
I would like to stop the program by pressing ESC at any time, how could it be?
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;
}