| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2397 人关注过本帖
标题:关于Linux下 setbuf(stdin,NULL)清空输入缓冲区无效
只看楼主 加入收藏
linan03
Rank: 4
等 级:业余侠客
威 望:5
帖 子:76
专家分:204
注 册:2012-10-27
结帖率:44.44%
收藏
已结贴  问题点数:20 回复次数:2 
关于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;
}

求解...
搜索更多相关主题的帖子: linux 缓冲区 功能键 Linux 
2013-04-16 22:17
linan03
Rank: 4
等 级:业余侠客
威 望:5
帖 子:76
专家分:204
注 册:2012-10-27
收藏
得分:0 
神啊~  你们在哪里~~
2013-04-17 23:05
pauljames
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:千里冰封
威 望:9
帖 子:1555
专家分:10000
注 册:2011-5-8
收藏
得分:20 
清空缓冲区?用fflush试试?

经常不在线不能及时回复短消息,如有c/单片机/运动控制/数据采集等方面的项目难题可加qq1921826084。
2013-04-18 06:50
快速回复:关于Linux下 setbuf(stdin,NULL)清空输入缓冲区无效
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.052752 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved