delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1999/08/18/18:16:51

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
From: "Philippe Noel" <pnoel AT socoint DOT com>
To: "Cygwin-List" <cygwin AT sourceware DOT cygnus DOT com>
Subject: FW: Porting getch() and kbdhit() ???
Date: Wed, 18 Aug 1999 18:15:00 -0400
Message-ID: <003f01bee9c7$1cf8be60$400010ac@philippe.socoint.com>
MIME-Version: 1.0
X-Priority: 3 (Normal)
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook 8.5, Build 4.71.2377.0
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
Importance: Normal

For your info. and archiving.  This way to emulate kbdhit() works fine to
me.  Thanks to Bill Luebkert for his help.

PN

-----Original Message-----
From: $Bill Luebkert [mailto:dbe AT wgn DOT net]
Sent: 18 August, 1999 5:24 PM
To: Philippe Noel
Subject: Re: Porting getch() and kbdhit() ???



/* kbhit.c - routine to check for keyboard char present */

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/time.h>

extern int tcgetattr (int fd, struct termios *t);
extern int tcsetattr (int fd, int action, const struct termios *t);

struct termios save_termios;

int main (int argc, char *argv[]);
int kbhit (void);
void init_term (void);
void restore_term (void);

/* *************************************************************************
*/

int
main (int argc, char *argv[]) {
	int ii;

setbuf (stdout, 0);		/* unbuffer stdout */
init_term ();

for (ii = 0; ii < 10; ii++) {

		int c;

	(void)printf ("type a char (or not)\n");
	c = kbhit ();
	if (c == 0) {
		(void)printf ("no hit\n");
	} else {
		(void)printf ("hit with %c\n", c);
	}
	sleep (2);
}

restore_term ();

return 0;

}

/* *************************************************************************
*/

void
init_term (void) {
	struct termios ios;

if (tcgetattr (STDIN_FILENO, &save_termios) < 0)
	exit errno;

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)
	exit errno;

}

/* *************************************************************************
*/

void
restore_term (void) {

if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &save_termios) < 0)
	exit errno;

}

/* *************************************************************************
*/

int
kbhit (void) {
        fd_set rfds;
        struct timeval tv;

FD_ZERO (&rfds);
FD_SET (0, &rfds);
tv.tv_sec = 0;
tv.tv_usec = 0;

select (1, &rfds, 0, 0, &tv);

if (FD_ISSET (0, &rfds)) {
	return getc (stdin);
}
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