Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
19 years ago

kbhit() in C

Hi,

I searching for an implementation of kbhit() from <conio.h> in C. I want to use this function for the Terminal in the eclipse IDE.

http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/rolleyes.gif Thx a lot for your help

2 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I&#39;m not sure if i understand you correctly but take a look at the "Board Diagnostics"-Template in Nios II IDE. There is a function called getchar() used for this.

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi cece,

    Thx for your answere. But getchart don&#39;t solve my problem, because its a blocking function. This means, if no key is pressed, the function getchar blocks until a key is pressed. With kbhit it is possible just to test, if an key was pressed(without blocking).

    -> with the help of <termios.h> it should be possible to programm a kbhit function, but the functions :

    - tcgetatt

    - tcsetattr

    from termios.h doesnt&#39;t work ??? Any other solutions are welcome

    Here the code :

    int getch()

    {

    static int ch = -1, fd = 0;

    struct termios neu, alt;

    fd = fileno(stdin);

    tcgetattr(fd, &alt);

    neu = alt;

    neu.c_lflag &= ~(ICANON|ECHO);

    tcsetattr(fd, TCSANOW, &neu);

    ch = getchar();

    tcsetattr(fd, TCSANOW, &alt);

    return ch;

    }

    int kbhit(void)

    {

    struct termios term, oterm;

    int fd = 0;

    int c = 0;

    tcgetattr(fd, &oterm);

    memcpy(&term, &oterm, sizeof(term));

    }