Non-blocking getchar()

This is a discussion on Non-blocking getchar() within the C Programming forums, part of the General Programming Boards category; Hello, Because the standard C library does not provide a non-blocking function to read the keyboard input, I decided to ...

  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 02:40 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 06: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, 01: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21