[英]Function that gets char instantly without pressing enter
I'm new to programming, and I try to program a menu in C++.我是编程新手,我尝试在 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
循环中,在按下ESC时中断,但我希望无需按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.我找到了
getch()
function 和<conio.h>
header,但人们说不要使用它,因为它仅适用于 Windows。
Is there any other method that I can get a character without pressing Enter , and it's multiplatform/meets coding ethic?有没有其他方法可以让我在不按Enter的情况下获得角色,并且它是多平台/符合编码道德的?
My operating system is Windows 11, but I would like to know the solution that works on other systems.我的操作系统是 Windows 11,但我想知道适用于其他系统的解决方案。
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.一般不能使用ISO C++标准库提供的函数来检测ESC键是否被按下。 However, most platforms provide a platform-specific API which does provide this functionality.
但是,大多数平台都提供特定于平台的 API,它确实提供了此功能。
On Microsoft Windows, you can use the functions _getch
, _getche
or ReadConsoleInput
.在 Microsoft Windows 上,您可以使用函数
_getch
、 _getche
或ReadConsoleInput
。
On Linux, you can use the ncurses library .在 Linux 上,可以使用ncurses 库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.