|
发表于 2004-9-14 10:19:02
|
显示全部楼层
[code:1]
#include <stdio.h>
#include <termios.h>
#include <sys/ioctl.h>
int Get_KeyValue()
{
struct termios stored_settings;
struct termios new_settings;
int key_value;
int key_press=0;
tcgetattr(0,&stored_settings);
new_settings = stored_settings;
new_settings.c_lflag &= (~ICANON);
new_settings.c_cc[VTIME] = 0;
new_settings.c_cc[VMIN] = 1;
tcsetattr(0,TCSANOW,&new_settings);
ioctl(0,FIONREAD,&key_press);
if(key_press)
{
key_value=getc(_IO_stdin);
}
else
{
key_value=0;
}
return key_value;
tcsetattr(0,TCSANOW,&stored_settings);
}
main()
{
int new_char=0;
while(1)
{
new_char=Get_KeyValue();
if(new_char)
{
if(new_char=='q')
break;
printf("you press key:'%c'\n",new_char);
}
usleep(10000);
}
}
[/code:1]
用ncurses库
[code:1]
#include <curses.h>
#include <stdio.h>
#include <stdlib.h>
main()
{
int c=0;
initscr();
cbreak();
noecho();
while(1)
{
c=getch();
if(c>='a'&&c<='z')
{
if(c=='q')
break;
printf("your input is:%c\n",c);
move(0,0);
refresh();
}
printf("asdfsdf\n");
refresh();
}
endwin();
}
[/code:1] |
|