Checking the keyboard status in Unix

Which function in Linux C can you use to check to see whether or not a keystroke is pending?  And more importantly, how can I use this function?
HexarAsked:
Who is Participating?
 
msmitsConnect With a Mentor Commented:
You should use the ioctl() system call on the file descriptor of the stdin.
On SUN Solaris, the manual page refers to the termio(7I) manual page which has a section about non-canonical mode input processing, which is what you want.
I believe I have some old code somewhere in which I did something like thing. I will try to find that for you, although it may use an older interface.

0
 
msmitsCommented:
#include <sys/ioctl.h>
#define fileno(p) ((p)->fd)

bool keypressed()
/* test whether a key if pressed */
{
      long l;
      int fd;

      fd = fileno(stdin);

      if (ioctl(fd, FIONREAD, &l) != -1)
            if (l > 0)
                  return TRUE;
      return FALSE;
}

0
All Courses

From novice to tech pro — start learning today.