终端非阻塞输入的设置例子:
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;
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;