keypressed()

Richard A. O'Keefe ok at quintus.UUCP
Fri Feb 19 13:35:02 AEST 1988


In article <4230 at june.cs.washington.edu>, pardo at june.cs.washington.edu (David Keppel) writes:
> In article <136 at forty2.UUCP> eschle at forty2.UUCP (Patrik Eschle) writes:
> [ how to do "char=keypressed()" returns 0 if none pressed? ]
> On BSD Un*x you can use the "select()" call to wait for a stream to become
> ready; if the "timeout" parameter is zero (NOT a zero pointer!) the call
> will return immediately.

The original poster simply wanted a way of finding out whether or not
anything had been typed.  BSD systems (and EUUG V7) have a very simple
way of making exactly that test:
	ioctl(file_descriptor, FIONREAD, &count_of_ready_characters);
{The name was different in EUUG V7, but the effect was similar.}
This sets count_of_ready_characters to the number of characters which
are available for input.  See "man 4 tty", which claims that "This
works for files, pipes, and terminals."

In System V it gets more complicated, but one approach is to switch
the terminal into non-blocking mode and try to read a character.
If you get one, there was one.  The snag is that you have to buffer
this character yourself.  But you don't have to wait.



More information about the Comp.lang.c mailing list