Subject: Re: Detecting keyboard press
To: Matthias Buelow <mkb@mukappabeta.de>
From: Matthias Buelow <mkb@mukappabeta.de>
List: netbsd-help
Date: 12/04/2001 11:04:45
I wrote:

>>Very simple thing I'm sure - sorry if this isn't the correct group to ask! I 
>>am writing some user space C++ code, and want to continuously perform a 
>>number of instructions until a keyboard press takes place. On other O/Ses I 
>>have done something like:
>> while(!kbhit())
>>  {
>>     // determine the meaning of life
>>  }
>>  // display the meaning of life
>>
>>What is the _simplest_ way to do this under NetBSD/i386 (1.5.1)
>
>character-cell applications.  The curses functions you're looking
>for are the *getch() family.  You might want to enable cbreak-mode
>with the cbreak() function, otherwise the standard-input will remain

Sorry, I haven't had my coffee yet and I seem to have misunderstood
your posting somewhat.  Actually, what you want to do is not to
wait for a single character press but to run code which can be
interrupted by a character press...  What you can do is to use
the select(2) or poll(2) functions to determine whether input is
available on stdin.  To operate in the polling mode you describe,
you have to set the timeout to 0, respectively.  Then select will
return immediately.  Note however, that your application will be
runnable all the time then, so that method is only applicable when
you're actually doing real work in the above loop (like number-
crunching or such).  If you just want to regularly poll for input
on one or more descriptors, then use a timeout higher than 0,
or make select/poll block until data is available.
In any case, once you know that data is available on stdin,
you can of course process it with the curses functions, if
desired (in the above example, you apparently throw away the
keypress data.)

--mkb