[英]Is there an equivelant for Pascal's readkey; in C or C++?
I am looking for a Console function that waits for the user to press a key. 我正在寻找一个等待用户按键的控制台功能。 I want it to be like Pascal's readkey; 我希望它像Pascal的readkey一样; as in a Console Only solution. 与仅限控制台解决方案一样。 No GUI library / Graphics Library / Windowing Library / WinApi Calls (Windows). 没有GUI库/图形库/窗口库/ WinApi调用(Windows)。 It should be cross-platform and (preferably) part of the C std library or C++ Class Library. 它应该是跨平台的,并且(最好)是C std库或C ++类库的一部分。 So is there any such function / class method etc. ? 那么有没有这样的函数/类方法等?
The C Standard library has no notion of a "keyboard buffer". C标准库没有“键盘缓冲区”的概念。 input/output is mostly line-based (triggered when ENTER is pressed). 输入/输出主要是基于行的(按下ENTER时触发)。
You have a few options: 你有几个选择:
As far as I know there is no portable solution for your item. 据我所知,您的商品没有便携式解决方案。 In windows, you can use the header <conio.h>
which has a function called getch()
for getting a char directly from the console. 在Windows中,您可以使用头文件<conio.h>
,它具有一个名为getch()
的函数,用于直接从控制台获取char。 If you are in Linux, then you can use the library ncurses . 如果您在Linux中,那么您可以使用库ncurses 。
If you're in a Windws run-time environment, you can use the non-standard C function kbhit( ). 如果您处于Windws运行时环境中,则可以使用非标准C函数kbhit()。 And there's a C-language Linux equivalent for Windows kbhit( )
. 还有一个与Windows kbhit( )
相当的C语言Linux。 The function does just what you want: it will tell you if a keyboard character has been typed without reading the character; 该功能正是您想要的:它会告诉您键盘字符是否在没有读取字符的情况下被键入; or, alternatively, will read and deliver to you one character iff one has been typed. 或者,如果输入了一个字符,则会读取并传送给您一个字符。 You can find it over here: 你可以在这里找到它:
http://pwilson.net/sample.html http://pwilson.net/sample.html
Scroll down to the paragraph headed "Single-character keyboard input for Linux and Unix" 向下滚动到“Linux和Unix的单字符键盘输入”这一段
HTH HTH
考虑_getch ?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.