Date: Sat, 18 May 2002 09:52:38 +0300 From: "Eli Zaretskii" Sender: halo1 AT zahav DOT net DOT il To: ahelm AT gmx DOT net Message-Id: <7999-Sat18May2002095237+0300-eliz@is.elta.co.il> X-Mailer: emacs 21.2.50 (via feedmail 8 I) and Blat ver 1.8.9 CC: djgpp AT delorie DOT com In-reply-to: <12024.1021660112@www22.gmx.net> (ahelm@gmx.net) Subject: Re: How to avoid kbhit() and getkey() ? References: <12024 DOT 1021660112 AT www22 DOT gmx DOT net> Reply-To: djgpp AT delorie DOT com Errors-To: nobody AT delorie DOT com X-Mailing-List: djgpp AT delorie DOT com X-Unsubscribes-To: listserv AT delorie DOT com Precedence: bulk > Date: Fri, 17 May 2002 20:28:32 +0200 (MEST) > From: ahelm AT gmx DOT net > > I'm using kbhit() and getkey() for simple user interaction > e.g. yes/no or "any key" questions. But this is very DJGPP > specific. Is there a way to write these things more portable, > especially for UNIX environments? > > There are 2 main functionalities: > 1) empty the keyboard buffer: > while(kbhit()) > { > getkey(); > } > > 2) wait for and read a single key > x = getkey(); The more portable way is to use termios and `select'. Set the keyboard to raw mode using `tcsetattr', drain the keyboard queue by calling `tcflush', and then read individual characters with `getc' and friends. Should you need to avoid getting stuck inside `getc' while it waits for the user to type something, use `select' to check if some input is available. Note that with this method, you gain portability to Posix systems, but lose portability to DOS/Windows platforms other than DJGPP and Cygwin.