Linux下的int getchar(void)在默认情况下需要Enter后才能继续。那么可否像DOS下那样即时返回的呢?
回答是肯定的:
- 使用curses库的getch()。
- read /dev/input/event0 (kernel >= 2.6)
- 将stdin改成unbuffered:
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
int getch (void)
{
int c = 0;
struct termios tm_old, tm;
int ret = 0;
ret = tcgetattr (STDIN_FILENO, &tm_old);
assert (ret==0);
memcpy (&tm, &tm_old, sizeof(tm));
tm.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL);
tcsetattr (STDIN_FILENO, TCSANOW, &tm);
c = fgetc(stdin);//getchar();
ret = tcsetattr (STDIN_FILENO, TCSANOW, &tm_old);
assert (ret==0);
return c;
}
//EOF
0 comments:
Post a Comment