Hello,
Because the standard C library does not provide a non-blocking function to read the keyboard input,
I decided to modify the lowlevel standard input in order to make the function getchar() non-blocking.
Here is the code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
struct termios initial_settings,
new_settings;
int main(int argc, char *argv[])
{
int n;
unsigned char key;
tcgetattr(0,&initial_settings);
new_settings = initial_settings;
new_settings.c_lflag &= ~ICANON;
new_settings.c_lflag &= ~ECHO;
new_settings.c_lflag &= ~ISIG;
new_settings.c_cc[VMIN] = 0;
new_settings.c_cc[VTIME] = 0;
tcsetattr(0, TCSANOW, &new_settings);
while(1)
{
n = getchar();
if(n != EOF)
{
key = n;
if(key == 27) /* Escape key pressed */
{
break;
}
/* do something useful here with key */
}
}
tcsetattr(0, TCSANOW, &initial_settings);
return(0);
}
Using this code, getchar() returns immediately and returns the key pressed by the user or, in case no key has been pressed it returns "EOF" which is equivalent to -1.
Now I want to port this code to the windows platform using the MinGW compiler.
My question is, because termios.h is not available on windows, which windows-api do I need in order to make the function getchar() non-blocking?
Regards.