This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Porting getch() and kbdhit() ???


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>

#ifndef STDIN_FILENO
# define STDIN_FILENO 0
#endif

int 
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;
}

int
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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]