该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include"/usr/workarea/cprogram/include/clsscr.h"
static struct termios old_st, new_st;
static int peek_character = -1;
void init_kb()
{
tcgetattr(0,&old_st);
new_st = old_st;
new_st.c_lflag &= ~ICANON;
new_st.c_lflag &= ~ECHO;
new_st.c_lflag &= ~ISIG;
new_st.c_cc[VMIN] = 1;
new_st.c_cc[VTIME] = 0;
tcsetattr(0, TCSANOW, &new_st);
}
int kb_hit()
{
char ch;
int nread;
if(peek_character != -1)
return 1;
new_st.c_cc[VMIN]=0;
tcsetattr(0, TCSANOW, &new_st);
nread = read(0,&ch,1);
new_st.c_cc[VMIN]=1;
tcsetattr(0, TCSANOW, &new_st);
if(nread == 1)
{
peek_character = ch;
return 1;
}
return 0;
}
int readch()
{
char ch;
if(peek_character != -1)
{
ch = peek_character;
peek_character = -1;
return ch;
}
read(0,&ch,1);
return ch;
}
void close_kb()
{
tcsetattr(0, TCSANOW, &old_st);
}
int main(int argc,char** argv)
{
int ch = 0;
init_kb();
while(ch != 'q')
{
sleep(1);
if(kb_hit())
{
ch = readch();
printf("%c\n",ch);
}
}
close_kb();
return 0;
}