Do you know any alternative to the function getch()
? because it is from the Conio library (which is not standard) and I would like to know if there is any standard alternative for C++.
I have this code:
do
{
tecla = getch();
} while (tecla != TECLA_ARRIBA && tecla != TECLA_ABAJO && tecla != ENTER);
I don't want a pause, I want to capture data like the example code.
Note: or otherwise, any code that emulates the function?
I understand that what you want is to read the direct inputs from the keys, bypassing the input buffer
std::cin
.In that case, the answer is simple: there is no standard way, since it depends on the Operating System you use.
If you're on Windows console emulation , you already know what to use:
conio.h
If you use a graphical application, you have no choice but to resort to the event system provided by the operating system, or to the highest level library you use.
The most portable way would be using the ncurses library . Although it is native to *nix systems, it can be used in Windows terminal emulators. In chapter 11 of
NCURSES Programming HOWTO
,Interfacing with the keyboard
, you have a complete example.As a last option, if you are on Linux, you can set the keyboard to raw mode , directly accessing the events it generates. You have a complete example in
Grab Raw Keyboard Input
.You can't do what you want. At least not pulling from the C++ standard.
C++ understands neither keyboards nor consoles but standard input and standard output. This change of concept, although it may seem trivial, has its implications:
The only option left for you is to pull OS-dependent libraries to perform this task:
conio.h
(Windows),ncurses
(Linux),pdcurses
(Windows), etc.You can use
cin.get( );
the libraryiostream
and put at your disposal the objectscin, cout
.example: