conio

How to implement getch() function of C in Linux?

荒凉一梦 提交于 2019-11-26 06:36:08
问题 In TurboC++, I can use the getch() function from conio.h . But in Linux, gcc doesn\'t provide conio.h . How can I get the functionality of getch() ? 回答1: Try this conio.h file: #include <termios.h> #include <unistd.h> #include <stdio.h> /* reads from keypress, doesn't echo */ int getch(void) { struct termios oldattr, newattr; int ch; tcgetattr( STDIN_FILENO, &oldattr ); newattr = oldattr; newattr.c_lflag &= ~( ICANON | ECHO ); tcsetattr( STDIN_FILENO, TCSANOW, &newattr ); ch = getchar();

Using kbhit() and getch() on Linux

北城以北 提交于 2019-11-26 05:36:53
问题 On Windows, I have the following code to look for input without interrupting the loop: #include <conio.h> #include <Windows.h> #include <iostream> int main() { while (true) { if (_kbhit()) { if (_getch() == \'g\') { std::cout << \"You pressed G\" << std::endl; } } Sleep(500); std::cout << \"Running\" << std::endl; } } However, seeing that there is no conio.h , whats the simplest way of achieving this very same thing on Linux? 回答1: The ncurses howto cited above can be helpful. Here is an