How can I read keyboard without stopping

Kevin Sheehan Consulting Poster Child kevin%kalli at Sun.COM
Wed Aug 10 02:54:30 AEST 1988


In article <813 at ms3.UUCP> isns02 at ms3.UUCP (Harris Reavin) writes:
>
>	I would like to know if it is possible to get input data from the
>keyboard while my program is constantly looping and displaying output to
>the screen. The input comes from a call to "popen()".
>I am using C, curses, and BSD4.2 on a VAX-780. I have only been
>able to enter input if the display stops for "getch()". This is not acceptable
>because I want the display to be continuous unless I am going to change one
>of the parameters.  For my PC I have Aspen Curses which has a "nodelay()" 
>function that indicates the presence of characters in the keyboard buffer. 
>Is there an some way to do this under UNIX?

ioctl(fd,FIONREAD, &count) will tell you the number of characters available
on a file descriptor.  A simple version of what I think you want:

/*
 *	returns -1 if no char, -2 for read error, otherwise char
 */

maygetchar(fd)
int fd;
{
	int	count;
	char	c;

	ioctl(fd, FIONREAD, &count);
	if (!count) {
	    return(-1);
	} else {
	    if(read(fd, &c, 1) != 1) return(-2);
	    return(c);
	}
}

		l & h,
		kev



More information about the Comp.unix.wizards mailing list