Sign in to follow this  

kbhit in linux

This topic is 4855 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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
}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
Sign in to follow this