Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++

kbhit() in C

Altera_Forum
Honored Contributor II
2,605 Views

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
0 Kudos
2 Replies
Altera_Forum
Honored Contributor II
1,548 Views

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.

0 Kudos
Altera_Forum
Honored Contributor II
1,548 Views

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)); 

}
0 Kudos
Reply