Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

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?
0
Hexar
Asked:
Hexar
  • 2
1 Solution
 
msmitsCommented:
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now