Is there an equivelant for Pascal's readkey; in C or C++?
-
22-01-2021 - |
Domanda
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; as in a Console Only solution. No GUI library / Graphics Library / Windowing Library / WinApi Calls (Windows). It should be cross-platform and (preferably) part of the C std library or C++ Class Library. So is there any such function / class method etc. ?
Soluzione
The C Standard library has no notion of a "keyboard buffer". input/output is mostly line-based (triggered when ENTER is pressed).
You have a few options:
Altri suggerimenti
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. If you are in Linux, then you can use the library ncurses.
If you're in a Windws run-time environment, you can use the non-standard C function kbhit( ). And there's a C-language Linux equivalent for Windows kbhit( )
. 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
Scroll down to the paragraph headed "Single-character keyboard input for Linux and Unix"
HTH
Considered _getch ?