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:
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.
struct termios initial_settings,
int main(int argc, char *argv)
unsigned char key;
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);
n = getchar();
if(n != EOF)
key = n;
if(key == 27) /* Escape key pressed */
/* do something useful here with key */
tcsetattr(0, TCSANOW, &initial_settings);
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?