LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Way of use of kbhit() function in gcc (https://www.linuxquestions.org/questions/programming-9/way-of-use-of-kbhit-function-in-gcc-776487/)

K-Z 12-17-2009 12:47 PM

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???

ForzaItalia2006 12-17-2009 05:17 PM

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 -

K-Z 12-17-2009 11:36 PM

Quote:

Originally Posted by ForzaItalia2006 (Post 3795961)
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 -

Thnx for the reply...tht is helpfull

Hko 12-18-2009 09:09 AM

You could also try the search of this forum.
There were several threads here about kbhit() over the years.

smeezekitty 12-18-2009 01:22 PM

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.