在TurboC++中,我可以使用conio.h
中的getch()
函数。但是在Linux中,gcc并没有提供conio.h
。如何获得getch()
的功能
发布于 2014-04-13 03:24:05
尝试此conio.h
文件:
#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();
tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
return ch;
}
/* reads from keypress, echoes */
int getche(void)
{
struct termios oldattr, newattr;
int ch;
tcgetattr( STDIN_FILENO, &oldattr );
newattr = oldattr;
newattr.c_lflag &= ~( ICANON );
tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
return ch;
}
您还可以使用gcc中的ncurses库来实现一些类似于conio.h
的功能。
发布于 2010-07-19 01:44:43
如果回显到屏幕不是问题,您可以尝试使用stdio.h
中的getchar()
。
发布于 2010-07-19 01:43:31
https://stackoverflow.com/questions/3276546
复制相似问题