can i test for keyboard input?

Tony Rems rembo at unisoft.UUCP
Sat Oct 6 08:53:20 AEST 1990


In article <1990Oct3.041737.2280 at watmath.waterloo.edu> mwnewman at watmsg.uwaterloo.ca (mike newman) writes:
>I would like to be able to respond dynamically to keyboard input
>(without doing getchar and waiting for the user to type something) by
>either:
>
>     1)	Having some kind of interrupt routine that will be called
>	every time a key is pressed.
>
>     2) Calling some OS routine that returns TRUE/FALSE if there
>	is/isn't any keyboard input waiting.  I know my little ol' ST
>	at home (bless it's heart :-) has such a call: does unix?
>
>     3) Any other way :-).
>
>This seems like it should be possible: musn't the shell do something
>along these lines to handle type ahead? (???)
>
>Please email replies: if there is interest, I will post a summary.
>

I think what you are looking for is the select(2) routine.  It
will allow you to open your tty and it will wait until there is
data available to be read, so it will return as soon as the user
types something.  

Another solution would be to set your terminal into cbreak mode
and this way input will come to your program without being buffered.
You can do this with ioctl(2).  This differs depending on whether
you are using BSD or SYS V. 

Final, if you want to get fancy, you can look into pseudo-ttys
or curses.  All these should be in your manual set.

If you can't find what you're looking for with these calls
let me know and I'll send you some sample code.

-Tony



More information about the Comp.unix.programmer mailing list