Linux에서 _kbhit() 구현
Unix/Linux 2013. 3. 15. 16:16Name : Linux에서 _kbhit() 구현
Compiler : gcc 4.1.2
Enviroment : Cent OS 5.5 (2.6.18)
Compile command : (default)
---------------------------------------------------------------------------
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
int kbhit(void)
{
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
if(ch != EOF)
{
ungetc(ch, stdin);
return 1;
}
return 0;
}
int main(void)
{
while(!kbhit())
puts("Press a key!");
printf("You pressed '%c'!\n", getchar());
return 0;
}
Compiler : gcc 4.1.2
Enviroment : Cent OS 5.5 (2.6.18)
Compile command : (default)
---------------------------------------------------------------------------
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
int kbhit(void)
{
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
if(ch != EOF)
{
ungetc(ch, stdin);
return 1;
}
return 0;
}
int main(void)
{
while(!kbhit())
puts("Press a key!");
printf("You pressed '%c'!\n", getchar());
return 0;
}
'Unix/Linux' 카테고리의 다른 글
tgz 압축/푸는 법 (0) | 2014.02.27 |
---|---|
.svn 파일 삭제하는법 (0) | 2014.02.27 |
Linux에서 getch() 구현 (0) | 2013.03.15 |
^M 문자 (0) | 2013.03.14 |
vi 편집기 사용법 (0) | 2013.03.14 |