[comp.lang.c] boolean function to tell if a key has been pressed

whoffman@topaz.UUCP (03/20/87)

Hello,

   I am seeking a way to tell whether or not a key has been pressed on
a keyboard. i.e something like this:
  
     while (1)
     { ...statements....
      if "a key has been pressed" {...statements}
      ...statements...}

the problem i keep running into is that getc,getchar etc..all WAIT for
a key to be pressed. I would just like to see IF one has been pressed.
Any help on this would be greatly appreciated. Thanks in advance...

								bill.

William R. Hoffman

Arpa: whoffman@topaz.rutgers.edu or whoffman@red.rutgers.edu
Uucp: ...{harvard | seismo | pyramid}!rutgers!topaz!whoffman

tr@thumper.UUCP (03/20/87)

In article <10260@topaz.RUTGERS.EDU>, whoffman@topaz.UUCP writes:
> 
> Hello,
> 
>    I am seeking a way to tell whether or not a key has been pressed on
> a keyboard. i.e something like this:
>   
>      while (1)
>      { ...statements....
>       if "a key has been pressed" {...statements}
>       ...statements...}
> 
> the problem i keep running into is that getc,getchar etc..all WAIT for
> a key to be pressed. I would just like to see IF one has been pressed.
> Any help on this would be greatly appreciated. Thanks in advance...
> 
> William R. Hoffman

Well, I gather you are using a minicomputer, so the routine I use
won't work for you.  In MS-DOS, the Microsoft and Lattice compilers
give you functions called kbkit() which take no arguments and return
either TRUE or FALSE.  Too bad because they work.

Anyway, why don't you use getchar(), check for EOF, and if there is a
character, unget() the character?  This would push it back onto the
input stream.

Tom Reingold

INTERNET:       tr@bellcore.com
--------
UUCP:
----		      watmath!clyde!bellcore\
                                ucbvax\       \
     lll-lcc\              ihnp4!mhuxt!ulysses!faline!flash!tr
      seismo!rutgers!mit-eddie!allegra/
-- 
Tom Reingold
Internet: tr@bellcore.com
Uucp: ..!allegra!ulysses!faline!flash!tr

edw@ius2.cs.cmu.edu.UUCP (03/22/87)

 In article <10260@topaz.RUTGERS.EDU>, whoffman@topaz.UUCP writes:
 > 
 > Hello,
 > 
 >    I am seeking a way to tell whether or not a key has been pressed on
 > a keyboard. i.e something like this:
 >   
 >      while (1)
 >      { ...statements....
 >       if "a key has been pressed" {...statements}
 >       ...statements...}
 > 
 > the problem i keep running into is that getc,getchar etc..all WAIT for
 > a key to be pressed. I would just like to see IF one has been pressed.
 > Any help on this would be greatly appreciated. Thanks in advance...
 > 
 > William R. Hoffman

  If you are on a unix os you can make a call to "select" and it will
tell you whether a file descriptor or list of files descriptor are ready
to read from, write to, or have excecption pending (See manual entry
for select for more details.)   Note also you can get the file descriptor
for a pointer to a file (FILE *fp)  with the macro fileno which is
defined in stdio.h.
   Also, if you are on a Unix os, you will want to make the appropriate
call to ioctl or stty so that characters are made available to your process
as soon as they are type, instead of buffering them until a new line is
type from the terminal (cbreak on or off, one of the two). 

					Eddie Wyatt

levy@ttrdc.UUCP (03/22/87)

In article <10260@topaz.RUTGERS.EDU>, whoffman@topaz.RUTGERS.EDU (The Skiing Fanatic!) writes:
<    I am seeking a way to tell whether or not a key has been pressed on
< a keyboard. i.e something like this:
<   
<      while (1)
<      { ...statements....
<       if "a key has been pressed" {...statements}
<       ...statements...}
< 
< the problem i keep running into is that getc,getchar etc..all WAIT for
< a key to be pressed. I would just like to see IF one has been pressed.
< Any help on this would be greatly appreciated. Thanks in advance...
< William R. Hoffman

Please tell us your machine and compiler when making net requests like this.
The answer differs depending on whether the environment is a PC (and upon the
PC compiler), System V, Berserkely Unix, VMS, IBM/CMS, etc.  Stuff having to
do with single key strokes is system dependent and may require special
terminal setups and/or exotic I/O calls and/or etc.
-- 
|------------dan levy------------|  Path: ..!{akgua,homxb,ihnp4,ltuxa,mvuxa,
|         an engihacker @        |		vax135}!ttrdc!ttrda!levy
| at&t computer systems division |  Disclaimer:  these views are all MINE!
|--------skokie, illinois--------|	MINE! MINE! HAHAHAHAHAHAHAHAHA!!!!!!

chris@mimsy.UUCP (Chris Torek) (03/22/87)

In article <1073@ius2.cs.cmu.edu> edw@ius2.cs.cmu.edu (Eddie Wyatt) writes:
>If you are on a unix os you can make a call to "select" ...

As this points out, the answer is system dependent.  In particular,
select() exists only on 4.2BSD and V8 derived systems.  (I understand
System VII ... no, make that System V release 3 [cannot change a
standard! :-) ] for the Vax has something equivalent, but harder
to use.)  Under 4.1BSD, one can use the FIONREAD ioctl.  Under
System III one can fiddle with VTIME or something like that.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
UUCP:	seismo!mimsy!chris	ARPA/CSNet:	chris@mimsy.umd.edu

decot@hpisod2.UUCP (03/24/87)

> As this points out, the answer is system dependent.  In particular,
> select() exists only on 4.2BSD and V8 derived systems.  (I understand
> System VII ... no, make that System V release 3 [cannot change a
> standard! :-) ] for the Vax has something equivalent, but harder
> to use.)  Under 4.1BSD, one can use the FIONREAD ioctl.  Under
> System III one can fiddle with VTIME or something like that.

Select(2) also exists in HP-UX, a SVID-conforming operating system.

Dave Decot
hpda!decot

josh@hi.UUCP (03/26/87)

-- 
Josh Siegel, Project  Asst.	|  Internet:	siegel@hc.dspo.gov
Electrical and Computer Eng.	|  UUCP:	hc!siegel
University of New Mexico	|  (505) 277-1611  (Lab)
Albuquerque, New Mexico 87131	|  (505) 277-2497  (Home)

jv@mhres.UUCP (03/28/87)

In article <2550008@hpisod2.HP.COM>, decot@hpisod2.HP.COM (Dave Decot) writes:
> Select(2) also exists in HP-UX, a SVID-conforming operating system.
> 
> Dave Decot
> hpda!decot

Yes it exists. But after we re-booted our HP9000 model 530 it told us
that the new select(2) system call would not work on our multiplexors ...
and these are the only terminal lines we have.
-- 

Johan Vromans @ Multihouse Research, Gouda, the Netherlands
      usenet: ..{seismo!}mcvax!mhres!jv