키입력을 바로 처리하는 linux용 코드입니다.
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define BAUDRATE B38400
int kbhit1( int stdfd );
int main()
{
char c;
int stdfd;
struct termios oldtio, newtio;
stdfd = fileno(stdin);
//------------------------------
tcgetattr(stdfd, &oldtio);
bzero(&newtio, sizeof(newtio));
newtio.c_cflag = BAUDRATE or CRTSCTS or CS8 or CLOCAL or CREAD;
newtio.c_iflag = IGNPAR or ICRNL;
newtio.c_oflag = 0;
newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 1;
tcflush(stdfd, TCIFLUSH);
tcsetattr(stdfd, TCSANOW, &newtio);
//------------------------------
printf("----\n");
while(1)
{
printf( "0x%x", c = kbhit1( stdfd ) );
if ( c == 0x20 ) // space
break;
//printf( "%x", c = getchar() );
}
tcsetattr(stdfd,TCSANOW,&oldtio); // 원래모드로 복귀
return 0;
}
int kbhit1( int stdfd )
{
fd_set readfs;
struct timeval Timeout;
unsigned char rv;
int res;
int cnt;
while(1)
{
printf(" loop working \r\n");
FD_SET(stdfd, &readfs);
Timeout.tv_sec = 3;
printf( "select:%d\r\n", res = select(stdfd + 1, &readfs, NULL, NULL, &Timeout));
if ( res < 0 )
{ perror(" Error on the select");
return -1; }
else if ( res > 0 )
{ if ( FD_ISSET(stdfd,&readfs) )
{ if ( read(stdfd,&rv,1) < 0 )
{ perror( "Error on read" );
return -1; }
return rv; }
else
return 0;
}
}
}
출처: http://kldp.org/node/35884
#include