0

This is the kbhit implementation that I found, but for some reason it just waits a key to be pressed instead of returning some result other than 0. It doesn't really function as kbhit...

int kbhit(void)
{
    struct timeval tv;
    fd_set read_fd;

    tv.tv_sec=0;
    tv.tv_usec=0;
    FD_ZERO(&read_fd);
    FD_SET(0,&read_fd);

    if(select(1, &read_fd, NULL, NULL, &tv) == -1)
        return 0;

    if(FD_ISSET(0,&read_fd))
        return 1;

    return 0;
}

Can anyone explain me what the problem is? I'm using Linux, btw.

I think you might have misunderstood me, and thought that it actully returns non zero value after a key stroke. my problem is that kbhit always WAITS for a key stroke.

Sason
  • 139
  • 1
  • 3
  • 6
  • Why didn't you use the kbhit implementation I sent you the link for in answer to your previous question ? – Paul R Oct 19 '10 at 18:44

1 Answers1

0

stdin is probably line-buffered. You'll need to switch it to unbuffered while trying to detect a keypress.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358