Path: utzoo!mnetor!uunet!seismo!sundc!pitstop!sun!quintus!ok From: ok@quintus.UUCP (Richard A. O'Keefe) Newsgroups: comp.lang.c Subject: Re: keypressed() Message-ID: <663@cresswell.quintus.UUCP> Date: 19 Feb 88 03:35:02 GMT References: <136@forty2.UUCP> <4230@june.cs.washington.edu> Organization: Quintus Computer Systems, Mountain View, CA Lines: 20 Summary: use FIONREAD In article <4230@june.cs.washington.edu>, pardo@june.cs.washington.edu (David Keppel) writes: > In article <136@forty2.UUCP> eschle@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.