I'm new to programming, and I try to program a menu in C++.
I want it to be in a while
loop, breaking when ESC is pressed, but I want the character to be read instantly without having to press Enter .
while (breaker != 27)
{
//menu based on switch(breaker)
}
I found the getch()
function and <conio.h>
header, but people say not to use it because it works only on Windows.
Is there any other method that I can get a character without pressing Enter , and it's multiplatform/meets coding ethic?
My operating system is Windows 11, but I would like to know the solution that works on other systems.
It is generally not possible to use the functions provided by the ISO C++ standard library to detect whether the ESC key has been pressed. However, most platforms provide a platform-specific API which does provide this functionality.
On Microsoft Windows, you can use the functions _getch
, _getche
or ReadConsoleInput
.
On Linux, you can use the ncurses library .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.