Keyboard input/Keypress check: Difference between revisions
Content added Content deleted
No edit summary |
(→{{header|C}}: flush stdin and stdout before reading a keypress) |
||
Line 587: | Line 587: | ||
=={{header|C}}== |
=={{header|C}}== |
||
For POSIX systems. Ctrl-C to stop:<lang C>#include <stdio.h> |
For POSIX systems. Ctrl-C to stop:<lang C>#include <stdio.h> |
||
#include <termios.h> |
#include <termios.h> /* general terminal interface: tcgetattr, tcsetattr, tcflush */ |
||
#include <unistd.h> /* synchronous I/O multiplexing: select, FD_CLR, FD_ISSET, FD_SET, FD_ZERO */ |
|||
#include <unistd.h> |
|||
#include <fcntl.h> |
#include <fcntl.h> |
||
Line 628: | Line 628: | ||
while(1) { |
while(1) { |
||
set_mode(1); |
set_mode(1); |
||
/* force C library buffers to be written to kernel buffers, |
|||
and flush pending input to avoid previously pressed keys */ |
|||
fflush(stdout); |
|||
while (!(c = get_key())) usleep(10000); |
while (!(c = get_key())) usleep(10000); |
||
printf("key %d\n", c); |
printf("key %d\n", c); |