Way of use of kbhit() function in gcc
Can you please tell me how to use kbhit function in gcc. I have searched in google and I hve found its code implementation in gcc as kbhit.h. Is there any need to compile kbhit.h or just add it into gcc's library??? If there is a need, how to compile it??? I have kbhit.h as
/* kbhit.h */ #include 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; } Is it correct??? |
Hey,
you don't really need a header file in this simple example. It could be "dangerous" anyway to include a function definition (different from function prototype declaration) in a header file if this header file is included in several source files (*.c), because you could then possibly get error messages during linking. To get this example compiled, create a file called kbhit.c (for gcc it's important to create a file with file extension *.c): ===== kbhit.c ===== #include <sys/select.h> 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; } int main (int argc, char *argv[]) { kbit(); exit(0); } ==== kbhit.c END ===== Then compile this file with the following command: # gcc kbhit.c -o kbhit And execute via: # ./kbhit I hope that answers your question ... - Andi - |
Quote:
|
You could also try the search of this forum.
There were several threads here about kbhit() over the years. |
The turbo C++ kbhit() returns 0xFFFF when the key is hit, not 1.
|
All times are GMT -5. The time now is 04:10 AM. |