Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

kbhit and conio.h

Status
Not open for further replies.

mover50

Programmer
Aug 3, 2004
77
US
As I understand it, the kbhit() function is not part of the Standard C++, but is a common extension that is supposed to be provided by most compilers. My documentation to use the header file conio.h for the kbhit() function, but when I do a compile that way, it says the conio.h is not found.
What do I do?

Thanks,

Kent
 
> As I understand it, the kbhit() function is not part of the Standard C++
Correct.

> but is a common extension that is supposed to be provided by most compilers.
There is no "supposed" - either it does or it doesn't.
Also, there is no reason to suppose that kbhit() provided by one compiler is in any way similar to the kbhit() of another compiler.
Most 16-bit DOS compilers provide something more or less similar to one another. Pretty much everything else does not.

> What do I do?
Say which operating system and compiler you do have.

--
 
Say which operating system and compiler you do have.

I guess the answer to that is:
OS is linux Fedora Core 2.
Compiler is the GNU GCC.

I have the GNU Compiler Collection Manual by Stallman, so I guess maybe I should inquire with them.

Unless you have a quick answer..

Thanks,

Kent
 
I thought <conio.h> was a Microsoft specific header file for functions like getch() & getche()... I've never heard of kbhit(). I really wish conio.h was part of the standard though.
 
Well this works on my RH8 box
Code:
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <sys/select.h>

struct termios oldterm, newterm;
void set_unbuffered ( void ) {
  tcgetattr( STDIN_FILENO, &oldterm );
  newterm = oldterm;
  newterm.c_lflag &= ~( ICANON | ECHO );
  tcsetattr( STDIN_FILENO, TCSANOW, &newterm );
}
void set_buffered ( void ) {
  tcsetattr( STDIN_FILENO, TCSANOW, &oldterm );
}

int kbhit ( void ) {
    int result;
    fd_set  set;
    struct timeval tv;

    FD_ZERO(&set);
    FD_SET(STDIN_FILENO,&set);  /* watch stdin */
    tv.tv_sec = 0;
    tv.tv_usec = 0;             /* don't wait */

    /* quick peek at the input, to see if anything is there */
    set_unbuffered();
    result = select( STDIN_FILENO+1,&set,NULL,NULL,&tv);
    set_buffered();

    return result == 1;
}

int main ( ) {
    int i;
    
    /* do something until a key is pressed */
    for ( i = 0 ; i < 10 && !kbhit() ; i++ ) {
        printf( "%d\n", i );
        sleep(1);
    }
    return 0;
}

> I have the GNU Compiler Collection Manual by Stallman, so I guess maybe I should inquire with them.
GCC is a portable compiler. They don't care about which OS you use any more than C itself does.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top