delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1999/08/18/02:19:30

Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT sourceware DOT cygnus DOT com>
List-Archive: <http://sourceware.cygnus.com/ml/cygwin/>
List-Post: <mailto:cygwin AT sourceware DOT cygnus DOT com>
List-Help: <mailto:cygwin-help AT sourceware DOT cygnus DOT com>,
<http://sourceware.cygnus.com/ml/#faqs>
Sender: cygwin-owner AT sourceware DOT cygnus DOT com
Delivered-To: mailing list cygwin AT sourceware DOT cygnus DOT com
Message-Id: <199908180616.BAA12085@mercury.xraylith.wisc.edu>
To: cygwin AT sourceware DOT cygnus DOT com
Subject: Re: Porting getch() and kbdhit() ???
In-Reply-To: Your message of "Tue, 17 Aug 1999 22:57:06 EDT."
<19990817225706 DOT B2983 AT cygnus DOT com>
Date: Wed, 18 Aug 1999 01:16:52 -0500
From: Mumit Khan <khan AT xraylith DOT wisc DOT EDU>

Chris Faylor <cygwin AT sourceware DOT cygnus DOT 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 AT sourceware DOT cygnus DOT com

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019