Hi!
Sorry if I am posting this in the wrong place.
I am trying to learn C.
I faced a issue in a code with conio.h and kbhit() and fixed it with a function "unix_text_kbhit(void)" I found on the web. I think I should use ncurses instead, anyway it is not the problem right now.
My issue is that putchar, in main, prints nothing... but it works fine on Linux.
This code is only for a teaching purpose, it is originally written for Windows.
I found mode_raw and unix_text_kbhit on the web to implement kbhit(), I am not able yet to understand them The problem probably comes from unix_text_kbhit since putchar works in others little codes i tried.
May somebody help, please
Here is the code:
Sorry if I am posting this in the wrong place.
I am trying to learn C.
I faced a issue in a code with conio.h and kbhit() and fixed it with a function "unix_text_kbhit(void)" I found on the web. I think I should use ncurses instead, anyway it is not the problem right now.
My issue is that putchar, in main, prints nothing... but it works fine on Linux.
This code is only for a teaching purpose, it is originally written for Windows.
I found mode_raw and unix_text_kbhit on the web to implement kbhit(), I am not able yet to understand them The problem probably comes from unix_text_kbhit since putchar works in others little codes i tried.
May somebody help, please
Here is the code:
Code:
#include <stdio.h>
#include <time.h>
#include <unistd.h> // kbhit
#include <sys/time.h> // kbhit
#include <termios.h> // raw
#include <unistd.h> // raw
void mode_raw(int activer)
{
static struct termios cooked;
static int raw_actif = 0;
if (raw_actif == activer)
return;
if (activer)
{
struct termios raw;
tcgetattr(STDIN_FILENO, &cooked);
raw = cooked;
cfmakeraw(&raw);
tcsetattr(STDIN_FILENO, TCSANOW, &raw);
}
else
tcsetattr(STDIN_FILENO, TCSANOW, &cooked);
raw_actif = activer;
}
int unix_text_kbhit(void)
{
struct timeval tv = { 0, 0 };
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);
return select(STDIN_FILENO + 1, &readfds, NULL, NULL, &tv) == 1;
}
int main()
{
int res, top=0;
int activer=1;
mode_raw(activer);
while (res!='q'){
if (unix_text_kbhit()){
res=getchar();
printf("touche %c pressee, val ascii : %d\n",res,res);
}
if(clock()>top+1000){ // contrôler le temps
top=clock();
putchar('0');
}
}
mode_raw(!activer);
return 0;
}