终端非阻塞输入的设置

终端非阻塞输入的设置例子:
set_noecho_mode(){
    struct termios ttystate;
    tcgetattr(0, &ttystate);    //read cur setting,0 is the terminal default file handler
    ttystate.c_lflag &= ~ICANON;// no buffering
    ttystate.c_lflag &= ~ECHO;  //no ehco either
    ttystate.c_cc[VMIN] =1;     /*get 1 char at a time */
    tcsetattr(0, TCSANOW,&ttystate);/*install settings*/
}

set_nodelay_mode(){
    int termflags;
    termflags= fcntl(0,F_GETFL);
    termflags |= O_NDELAY;
   /*开启非阻塞模式,非阻塞就是ATM机不用一定得等待用户输入才继续运行下面的语句。这和以前的编程不一样*/
    fcntl(0,F_SETFL,termflags);
}


tty_mode(int how){
    static struct termios original_mode;
    static int original_flags;
    if(how == 0){
    tcgetattr(0,&original_mode);
    original_flags = fcntl(0,F_GETFL);
    }
    else{
    tcsetattr(0,TCSANOW ,&original_mode);
    fcntl(0,F_SETFL,original_flags);
    }
}


#include<stdio.h>
#include<termios.h>//设置终端的头文件,包含termios结构定义
#include<fcntl.h>
#include<string.h>

int main(int argc,char **argv)
{
    int keyvalue;
    int bloop=1;
    tty_mode(0);
    set_cr_noecho_mode();
    set_nodelay_mode();
    while(bloop)   
    {
           keyvalue = getchar();
           command(ret);                                   
        ....
     }
    tty_mode(1);
    return 0;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值