Porting getch() and kbdhit() ???

Mumit Khan khan@xraylith.wisc.EDU
Tue Aug 31 23:49:00 GMT 1999

Chris Faylor <cygwin@sourceware.cygnus.com> writes:
> Well, that's only partly true.  I have no qualms about including stuff
> that people need in Cygwin.  Since these two functions have been
> requested repeatedly, I'd certainly consider applying a patch which adds
> this functionality to cygwin.
> There.  I've asked for contributions.  That should kill this thread.

How about the following extermely inefficient but portable POSIX kbdhit 
(includes testcase)?

I have no idea what getch() Philippe is referring to, but probably not
the one that's typically provided by (n)curses.

----- cut from here to end.

/* kbdhit.c */

#include <stdio.h>
#include <termios.h>

# define STDIN_FILENO 0

kbdhit ()
  struct termios save_termios;
  struct termios ios;
  int c;

  if (tcgetattr (STDIN_FILENO, &save_termios) < 0)
    return EOF;
  ios = save_termios;
  ios.c_lflag &= ~(ICANON | ECHO);
  ios.c_cc[VMIN] = 1;
  ios.c_cc[VTIME] = 0;

  if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &ios) < 0)
    return EOF;

  if (read (STDIN_FILENO, &c, 1) != 1)
    c = EOF;

  if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &save_termios) < 0)
    return EOF;
  return c;

main ()
  int i;
  char c;

  fputs ("Enter key to continue: ", stdout);
  fflush (stdout);

  c = kbdhit ();
  putchar (c);

  putchar ('\n');
  return 0;

Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com

More information about the Cygwin mailing list