Is there a way to read input directly from the keyboard in standard C ++?

And I know that there is std::cin , but this requires the user to enter a string, and then press ENTER. Is there a way to simply get the next key that is pressed without pressing ENTER to confirm.

+2
source share
3 answers

you can use

 #include <conio.h> 

and then catch char with cases like

 char c; if (_kbhit()) { c = getch(); switch(c) { case '\0H' : cout << "up arrow key!" << endl; break; } } 

Beware : I have not tried ... and remember to put it all on "while (true)" to test.

+1
source

What you are looking for is related to console management and is OS dependent. If you are on a UNIX-based OS, check out the curses library , and on Windows there are getch() and kbhit() functions from <conio.h> .

+10
source

It seems that the highest answer is a bit outdated.

The ncurses library (based on the curses library mentioned) is a portable version available for operating systems, windows, and other unix and linux-based applications.

It supports a large number of interface interfaces.

+1
source

Source: https://habr.com/ru/post/1343887/


All Articles