LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-17-2009, 12:47 PM   #1
K-Z
LQ Newbie
 
Registered: Dec 2009
Posts: 11

Rep: Reputation: 0
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???

Last edited by K-Z; 12-17-2009 at 12:53 PM.
 
Old 12-17-2009, 05:17 PM   #2
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
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 -
 
Old 12-17-2009, 11:36 PM   #3
K-Z
LQ Newbie
 
Registered: Dec 2009
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ForzaItalia2006 View Post
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
 
Old 12-18-2009, 09:09 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
You could also try the search of this forum.
There were several threads here about kbhit() over the years.
 
Old 12-18-2009, 01:22 PM   #5
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
The turbo C++ kbhit() returns 0xFFFF when the key is hit, not 1.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
kbhit() iceman47 Programming 5 09-20-2009 12:34 PM
Regarding the kbhit kurrinchi Programming 2 01-30-2008 11:00 AM
function in a function in C compiling with gcc joel2001k Programming 5 06-23-2007 08:18 PM
kbhit () Mohsen Programming 2 03-29-2003 08:36 PM
Borland's kbhit() iceman47 Linux - General 1 10-28-2002 02:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration