什么是Linux中的Getch()&getche()?我無法在Linux中找到conio.h的等效頭文件。有什么可供選擇的嗎?getch() & getche()Linux中的函數(shù)?我想做一個開關案例基礎菜單,用戶將提供他的選擇,只要按一個鍵&進程應該向前移動。我不想讓用戶按回車后,按下他的選擇。
3 回答

qq_遁去的一_1
TA貢獻1725條經(jīng)驗 獲得超8個贊
#include <termios.h>#include <stdio.h>static struct termios old, new;/* Initialize new terminal i/o settings */void initTermios(int echo) { tcgetattr(0, &old); /* grab old terminal i/o settings */ new = old; /* make new settings same as old settings */ new.c_lflag &= ~ICANON; /* disable buffered i/o */ if (echo) { new.c_lflag |= ECHO; /* set echo mode */ } else { new.c_lflag &= ~ECHO; /* set no echo mode */ } tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */}/* Restore old terminal i/o settings */void resetTermios(void) { tcsetattr(0, TCSANOW, &old);}/* Read 1 character - echo defines echo mode */char getch_(int echo) { char ch; initTermios(echo); ch = getchar(); resetTermios(); return ch;}/* Read 1 character without echo */char getch(void) { return getch_(0);}/* Read 1 character with echo */char getche(void) { return getch_(1);}/* Let's test it out */int main(void) { char c; printf("(getche example) please type a letter: "); c = getche(); printf("\nYou typed: %c\n", c); printf("(getch example) please type a letter..."); c = getch(); printf("\nYou typed: %c\n", c); return 0;}

POPMUISE
TA貢獻1765條經(jīng)驗 獲得超5個贊
char getch(){ /*#include <unistd.h> //_getch*/ /*#include <termios.h> //_getch*/ char buf=0; struct termios old={0}; fflush(stdout); if(tcgetattr(0, &old)<0) perror("tcsetattr()"); old.c_lflag&=~ICANON; old.c_lflag&=~ECHO; old.c_cc[VMIN]=1; old.c_cc[VTIME]=0; if(tcsetattr(0, TCSANOW, &old)<0) perror("tcsetattr ICANON"); if(read(0,&buf,1)<0) perror("read()"); old.c_lflag|=ICANON; old.c_lflag|=ECHO; if(tcsetattr(0, TCSADRAIN, &old)<0) perror ("tcsetattr ~ICANON"); printf("%c\n",buf); return buf; }
remove the last printf if you dont want the char to be displayed
添加回答
舉報
0/150
提交
取消