Advertisement

kbhit in linux

Started by August 25, 2004 03:34 PM
3 comments, last by gregs 20 years, 2 months ago
i am literally exhuasted trying to hunt a better "kbhit" equivalent in linux. The first one i found was in FMOD api.. they provide a header called "wincompat.h" which implements the "kbhit." But unfortunatley that kbhit doesn't work so well.. it screws up at times. Then i did a bit of googling and found another implementation of "kbhit" but that too freezes my console. Now where do i go from here... ? :S
Z
from a newsgroup posting by Andreas Kochenburger:

int WaitKey(void) /* return true when keyboard event is in queue */{#ifdef LINUX    int i;    FD_ZERO(&fds);    FD_SET(STDIN_FILENO, &fds);    tv.tv_sec = tv.tv_usec = 0;    i = select(1, &fds, NULL, NULL, &tv);    if (i == -1) return(MFALSE);    if (FD_ISSET(STDIN_FILENO, &fds)) return(MTRUE);    return(MFALSE); #else    return(kbhit());#endif}
Advertisement
thanx, ur code seems to be working .. but consider the following code

while( 1 ){if(WaitKey())   getch();}


once the getch is peformed the, the waitkey shouldn't return true unless a key is again pressed. But itz screwing at that point, the WaiKey still returning true.. ! By the way, i am using the getch fuction of curses.h so i am just wondering whether the getch of curses is conflicting with the custom WaitKey
Z
What key did you pressed?
Perhaps I'm wrong (the last time I used these function was years ago) but with extended keys getch() may return a code and then another byte should be captured in order to know the very key. If this is the case, your custom kbhit() may read this second byte. But this could be under win... just an Idea...
They are using two different methods to perform the same action. Removing the key in getch does not necessarily remove it from the key's file stream.

This topic is closed to new replies.

Advertisement