#include
#include
#include
#include
int kbhit (void)
{
char c;
static struct termios new_term;
static struct termios old_term;
static int handle;
static int init = 0;
fflush(stdout);
if (init == 0) {
init = 1;
if ((handle = open("/dev/tty", O_RDWR | O_SYNC)) == 0) {
fprintf(stderr, "Nelze cist z terminalu!\n");
exit(1);
} else {
tcgetattr(handle, &old_term);
new_term = old_term;
new_term.c_lflag &= ~ICANON;
new_term.c_lflag &= ~ECHO;
new_term.c_cc[VMIN] = 0;
new_term.c_cc[VTIME] = 1;
}
}
tcsetattr(handle, TCSANOW, &new_term);
if (read(handle, &c, 1) <= 0) {
c = 0;
} else {
ungetc(c, stdin);
}
tcsetattr(handle, TCSANOW, &old_term);
return c;
}
int main (void)
{
int c;
while (1) {
putchar('>');
if (( c = kbhit()) != 0) {
printf ("Stisknuta klavesa %c\n", c);
if (c == 'z') break;
}
}
return 0;
}
Takto vypadá ta zmiňovaná funkce z knihy. Jak autor píše, je dost pomalá, ale je to možnost, kterou lze použít.
zigi psal o použití funkce scanf, vypadalo by to nějak takto: scanf ("%*c"); (hvězdička znamená, že se načtená hodnota zahodí a nebude se nikam ukládat), ale řekl bych, že je to jenom složitější alternativa k getchar()