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]

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


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@wgn.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@sourceware.cygnus.com


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