Lendo o teclado de forma não bloqueante

Se você precisa ler o teclado dentro de um laço de repetição de forma não bloqueante, então está dica é para você:

#include < stdio.h >
#include < unistd.h >
#include < termios.h >
#include < stdlib.h >
int  main (void) {
char chr;
int result;
struct termios term;
tcgetattr(STDIN_FILENO, &term);
term.c_lflag &= ~ICANON;
term.c_cc[VMIN]  = 0;
term.c_cc[VTIME]  = 1;
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &term) <
perror("We failed to change terminal settings\n");
//Polling the keyboard
while ( 1 ) {
result =read(STDIN_FILENO, &chr, 1);
if (result ==1) {
//Do something with input
printf("%c", chr);

              	}
//Do something ”more important”
printf("."); fflush(STDIN_FILENO); } }

Source: http://users.metropolia.fi/~hannuvl/material/realtime_prog/part8.ppt

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s