Porting getch() and kbdhit() ???

David W. Alderman dave@mmrd.com
Wed Aug 18 08:28:00 GMT 1999

Hi guys,

kbhit and getch were defined in Xenix, I believe.  They were included to 
allow greater compatibility with DOS  :-)
Unfortunately, both functions do not fit well with a mode-oriented 
ioctl-based input stream, although I guess you could do a kbhit() 
equivalent in cbreak mode, which is appropriate mode for getch().

I suspect you  (Philippe?) have a program loop like this:

while ( TRUE )
     if kbhit()
         inchar = getch();
         <Put entered character in its place>

     <Most of program's real work>

Under ioctl(), this could be implemented as:

ioctl(<Save current settings>)  // So we can restore later
ioctl(<RAW mode>)  //  So read will return if no key has been pressed

while (TRUE)
     if (read(0, inbuf, 1)  // Returns 0 if no key pressed
         <Put entered character in its place>

     <Most of program's real work>
ioctl(<Restore current settings>)  //  Be nice to your parent shell!

The correct settings for ioctl() under cygwin are left as an exercise for 
the implementer  :-)

Please forgive any obvious errors (or any non-obvious ones).

At 06:11 PM 8/17/1999 -0400, Lincoln, W. Terry wrote:
>Those two functions are not POSIX now, are they?  Sounds DOS/WINDOZISH to me
>Cygwin is for porting Unix(POSIX) apps to Win32.


> > -----Original Message-----
> > From: Philippe Noel [ mailto:pnoel@socoint.com ]


> > I'm currently trying to port a source code that use getch()
> > and kbdhit()
> > under Linux.
> > It really do not seems to be supported by Cygwin.  Is there a
> > reason for
> > this?
> >
> > And, is there a way to do the same thing as this (very
> > common) code under
> > Cygwin?
> >
> > if( kbdhit() )
> >    key = getch();
> >

Dave Alderman - Democracy should not be capital intensive.
Business: dave@mmrd.com
Personal: dwa@atlantic.net -or- dwa@netcommander.com

Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com

More information about the Cygwin mailing list