How to make it so that when you press the ESC key, without pressing then the Enter key, in getchar (e) for example, did the program end?

  • one
    Need to analyze keystrokes. If the ESC key is pressed, then exit the program. - pepsicoca1
  • one
    Use _getch() from conio.h - Harry

1 answer 1

 #include <conio.h> if (_getch() == 27) { exit(0); } 

By the way, if you want to intercept pressing ESC not only in the console, you can use the GetAsyncKeyState () function from Windows.h

 for (int i = 0; i < 255; i++) { int state = GetAsyncKeyState(i); if (state == 1 || state == -32767 && i == 27) exit(0); } 

Key Codes