Thread: Non-blocking getchar()

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    15

    Non-blocking getchar()

    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.
    Last edited by Theodoor; 09-23-2010 at 01:40 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Read the FAQ perhaps?
    There's a "how to read keyboard without waiting" one, I'm reasonably sure.

    Though some other mousy posters would just claim that was a guess.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I don't understand "while (getchar() != '\n');"
    By valedor in forum C++ Programming
    Replies: 13
    Last Post: 09-08-2009, 05:12 PM
  2. getchar() problem
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 02:49 PM
  3. getchar buffer size
    By oncemyway in forum C Programming
    Replies: 3
    Last Post: 08-02-2005, 12:49 AM
  4. help with getchar lol
    By Taco Grande in forum C Programming
    Replies: 5
    Last Post: 03-18-2003, 09:25 PM
  5. Can anybody take a look at this?
    By TerryBogard in forum C Programming
    Replies: 10
    Last Post: 11-21-2002, 01:11 PM