关于Linux下 setbuf(stdin,NULL)清空输入缓冲区无效
在linux下 用gcc 编译:以下是各个功能键的ascii码值:
Tab: 9
ENTER: 13
Space: 32
Home: 27 91 49 126
Delete: 27 91 51 126
End: 27 91 52 126
PageUp: 27 91 53 126
PageDown: 27 91 54 126
F01: 27 91 91 65
F02: 27 91 91 66
F03: 27 91 91 67
F04: 27 91 91 68
F05: 27 91 91 69
F06: 27 91 49 55 126
F07: 27 91 49 56 126
F08: 27 91 49 57 126
F09: 27 91 50 48 126
F10: 27 91 50 49 126
F11: 27 91 50 51 126
F12: 27 91 50 52 126
向上: 27 91 65
向下: 27 91 66
向右: 27 91 67
向左: 27 91 68
代码如下:
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
//实现windows下getch()的功能
int getch(void)
{
int ret;
struct termios term,term_old;
if(tcgetattr(STDIN_FILENO,&term)==-1)
{
printf("Error!");
return 0;
}
term_old=term;
cfmakeraw(&term);
tcsetattr(STDIN_FILENO,TCSANOW,&term);
ret=fgetc(stdin);
tcsetattr(STDIN_FILENO,TCSANOW,&term_old);
return ret;
}
//对键盘按下的键做判断(不完全)
int Press_Judge(void)
{
int ch;
ch=getch();
switch(ch)
{
case 9:
printf("Tab\n");
return ch;
case 13:
printf("Enter\n");
return ch;
case 32:
printf("Space\n");
return ch;
case 27:
break;
default:
printf("[%c]",ch);
return ch;
}
ch=getch(); //第二层的都是91
ch=getch();
switch(ch)
{
case 51:
printf("Delete\n");
return ch;
case 52:
printf("End\n");
return ch;
case 53:
printf("PageUp\n");
return ch;
case 54:
printf("PageDown\n");
return ch;
case 65:
printf("Up\n");
return ch;
case 66:
printf("Down\n");
return ch;
case 67:
printf("Right\n");
return ch;
case 68:
printf("Left\n");
return ch;
default:
break;
}
ch=getch();
switch(ch)
{
case 48:
printf("F9\n");
return ch;
case 49:
printf("F10\n");
return ch;
case 51:
printf("F11\n");
return ch;
case 52:
printf("F12\n");
return ch;
case 55:
printf("F6\n");
return ch;
case 56:
printf("F7\n");
return ch;
case 57:
printf("F8\n");
return ch;
case 65:
printf("F1\n");
return ch;
case 66:
printf("F2\n");
return ch;
case 67:
printf("F3\n");
return ch;
case 68:
printf("F4\n");
return ch;
case 69:
printf("F5\n");
return ch;
case 126:
printf("Home\n");
return ch;
default:
printf("Unknow!!!\n");
return ch;
}
return 0;
}
int main(int argc, char *argv[])
{
int ch;
printf("Input (\"Home\" over):\n");
while(ch!=126)
{
ch=Press_Judge();
printf("%d\n",ch); //测试用 查看最后传出的码值
setbuf(stdin,NULL); //就是这里 清空输入缓冲区无效
}
//getch();
return 0;
}
求解...