C++ Alternativa a getch()?


Do you know of an alternative to the getch() function? 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:

      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 if not, any code that emulates the function?


I understand that what you want is to read the direct inputs of 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 are in the 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 that 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 the 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 in raw mode, directly accessing the events it generates. You have a complete example at Grab Raw Keyboard Input .

Scroll to Top