什么是Linux中的Getch()&getche()?

什么是Linux中的Getch()&getche()?

我无法在Linux中找到conio.h的等效头文件。

有什么可供选择的吗?getch() & getche()Linux中的函数?

我想做一个开关案例基础菜单,用户将提供他的选择,只要按一个键&进程应该向前移动。我不想让用户按回车后,按下他的选择。


狐的传说
浏览 787回答 3
3回答

qq_遁去的一_1

#include&nbsp;<termios.h>#include&nbsp;<stdio.h>static&nbsp;struct&nbsp;termios&nbsp;old,&nbsp;new;/*&nbsp;Initialize&nbsp;new&nbsp;terminal&nbsp;i/o&nbsp;settings&nbsp;*/void&nbsp;initTermios(int&nbsp;echo)&nbsp;{ &nbsp;&nbsp;tcgetattr(0,&nbsp;&old);&nbsp;/*&nbsp;grab&nbsp;old&nbsp;terminal&nbsp;i/o&nbsp;settings&nbsp;*/ &nbsp;&nbsp;new&nbsp;=&nbsp;old;&nbsp;/*&nbsp;make&nbsp;new&nbsp;settings&nbsp;same&nbsp;as&nbsp;old&nbsp;settings&nbsp;*/ &nbsp;&nbsp;new.c_lflag&nbsp;&=&nbsp;~ICANON;&nbsp;/*&nbsp;disable&nbsp;buffered&nbsp;i/o&nbsp;*/ &nbsp;&nbsp;if&nbsp;(echo)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new.c_lflag&nbsp;|=&nbsp;ECHO;&nbsp;/*&nbsp;set&nbsp;echo&nbsp;mode&nbsp;*/ &nbsp;&nbsp;}&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new.c_lflag&nbsp;&=&nbsp;~ECHO;&nbsp;/*&nbsp;set&nbsp;no&nbsp;echo&nbsp;mode&nbsp;*/ &nbsp;&nbsp;} &nbsp;&nbsp;tcsetattr(0,&nbsp;TCSANOW,&nbsp;&new);&nbsp;/*&nbsp;use&nbsp;these&nbsp;new&nbsp;terminal&nbsp;i/o&nbsp;settings&nbsp;now&nbsp;*/}/*&nbsp;Restore&nbsp;old&nbsp;terminal&nbsp;i/o&nbsp;settings&nbsp;*/void&nbsp;resetTermios(void)&nbsp;{ &nbsp;&nbsp;tcsetattr(0,&nbsp;TCSANOW,&nbsp;&old);}/*&nbsp;Read&nbsp;1&nbsp;character&nbsp;-&nbsp;echo&nbsp;defines&nbsp;echo&nbsp;mode&nbsp;*/char&nbsp;getch_(int&nbsp;echo)&nbsp;{ &nbsp;&nbsp;char&nbsp;ch; &nbsp;&nbsp;initTermios(echo); &nbsp;&nbsp;ch&nbsp;=&nbsp;getchar(); &nbsp;&nbsp;resetTermios(); &nbsp;&nbsp;return&nbsp;ch;}/*&nbsp;Read&nbsp;1&nbsp;character&nbsp;without&nbsp;echo&nbsp;*/char&nbsp;getch(void)&nbsp;{ &nbsp;&nbsp;return&nbsp;getch_(0);}/*&nbsp;Read&nbsp;1&nbsp;character&nbsp;with&nbsp;echo&nbsp;*/char&nbsp;getche(void)&nbsp;{ &nbsp;&nbsp;return&nbsp;getch_(1);}/*&nbsp;Let's&nbsp;test&nbsp;it&nbsp;out&nbsp;*/int&nbsp;main(void)&nbsp;{ &nbsp;&nbsp;char&nbsp;c; &nbsp;&nbsp;printf("(getche&nbsp;example)&nbsp;please&nbsp;type&nbsp;a&nbsp;letter:&nbsp;"); &nbsp;&nbsp;c&nbsp;=&nbsp;getche(); &nbsp;&nbsp;printf("\nYou&nbsp;typed:&nbsp;%c\n",&nbsp;c); &nbsp;&nbsp;printf("(getch&nbsp;example)&nbsp;please&nbsp;type&nbsp;a&nbsp;letter..."); &nbsp;&nbsp;c&nbsp;=&nbsp;getch(); &nbsp;&nbsp;printf("\nYou&nbsp;typed:&nbsp;%c\n",&nbsp;c); &nbsp;&nbsp;return&nbsp;0;}只需复制这些函数并使用它。我很久以前就在谷歌上找到了这个片段,我保存了它,最后我为你打开了很长一段时间,希望它能帮助你!谢谢

POPMUISE

char&nbsp;getch(){ &nbsp;&nbsp;&nbsp;&nbsp;/*#include&nbsp;<unistd.h>&nbsp;&nbsp;&nbsp;//_getch*/ &nbsp;&nbsp;&nbsp;&nbsp;/*#include&nbsp;<termios.h>&nbsp;&nbsp;//_getch*/ &nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;buf=0; &nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;termios&nbsp;old={0}; &nbsp;&nbsp;&nbsp;&nbsp;fflush(stdout); &nbsp;&nbsp;&nbsp;&nbsp;if(tcgetattr(0,&nbsp;&old)<0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror("tcsetattr()"); &nbsp;&nbsp;&nbsp;&nbsp;old.c_lflag&=~ICANON; &nbsp;&nbsp;&nbsp;&nbsp;old.c_lflag&=~ECHO; &nbsp;&nbsp;&nbsp;&nbsp;old.c_cc[VMIN]=1; &nbsp;&nbsp;&nbsp;&nbsp;old.c_cc[VTIME]=0; &nbsp;&nbsp;&nbsp;&nbsp;if(tcsetattr(0,&nbsp;TCSANOW,&nbsp;&old)<0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror("tcsetattr&nbsp;ICANON"); &nbsp;&nbsp;&nbsp;&nbsp;if(read(0,&buf,1)<0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror("read()"); &nbsp;&nbsp;&nbsp;&nbsp;old.c_lflag|=ICANON; &nbsp;&nbsp;&nbsp;&nbsp;old.c_lflag|=ECHO; &nbsp;&nbsp;&nbsp;&nbsp;if(tcsetattr(0,&nbsp;TCSADRAIN,&nbsp;&old)<0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror&nbsp;("tcsetattr&nbsp;~ICANON"); &nbsp;&nbsp;&nbsp;&nbsp;printf("%c\n",buf); &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;buf; &nbsp;}复制这个函数并使用它,别忘了包括remove the last printf if you dont want the char to be displayed
打开App,查看更多内容
随时随地看视频慕课网APP