Message-ID: <3CE6268F.B82FB4BF@yahoo.com> From: CBFalconer Organization: Ched Research X-Mailer: Mozilla 4.75 [en] (Win98; U) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: comp.os.msdos.djgpp Subject: Re: How to avoid kbhit() and getkey() ? References: <12024 DOT 1021660112 AT www22 DOT gmx DOT net> <7999-Sat18May2002095237+0300-eliz AT is DOT elta DOT co DOT il> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 58 Date: Sat, 18 May 2002 10:14:15 GMT NNTP-Posting-Host: 12.90.167.104 X-Complaints-To: abuse AT worldnet DOT att DOT net X-Trace: bgtnsc04-news.ops.worldnet.att.net 1021716855 12.90.167.104 (Sat, 18 May 2002 10:14:15 GMT) NNTP-Posting-Date: Sat, 18 May 2002 10:14:15 GMT To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Eli Zaretskii wrote: > > > 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. IMNSHO the portable way is to create your own function int chready(FILE *f); which returns 0 unless it knows that fgetc(f) will return immediately. You have given a couple of methods of implementing chready. Once you have this all the rest is under control, although you may have to eschew some standard library calls to avoid getting hung up awaiting user input. Note that code such as: while (whatever) { dothings(); if (chready(stdin)) handleit(); domorethings(); } will run without interruption if chready always returns 0. So you can write code that is keyboard interruptible on platforms with the capability and otherwise ignores it. -- Chuck F (cbfalconer AT yahoo DOT com) (cbfalconer AT worldnet DOT att DOT net) Available for consulting/temporary embedded and systems. USE worldnet address!