How to do non-blocking keyboard input

Bruno Pape root at sgzh.uucp
Fri Jun 15 19:50:38 AEST 1990


In article <14670 at thorin.cs.unc.edu> taylorr at glycine.cs.unc.edu (Russell Taylor) writes:
>
>	I want to do non-blocking keyboard I/O in a program.  Basically, the
>routine will read a character from the keyboard if there is one and return
>NULL if there is not.  This is done in a tight loop with reads and writes
>to other devices.  How to I get a character from the keyboard if there is
>one and a NULL if there is not?
>
>	Thanks,
>	Russell Taylor
>	taylorr at cs.unc.edu

This will do what you want.  It is short and simple, but maybe not so portable.

I know there are others that prefer select over ioctl, and we know who each
other are, but you can choose the method best suited to your own needs.

Have Fun,

Bruno

----------------------- begin program --------------------------------------
#include <sys/termio.h>

main()
{
	struct	termio	kbd, kbd_save;
	char	buff;

	if ( ioctl( 0, TCGETA, &kbd_save ) < 0 ) perror( "ioctl" );

	if ( ioctl( 0, TCGETA, &kbd ) < 0 ) perror( "ioctl" );

	kbd.c_lflag = kbd.c_cc[VTIME] = kbd.c_cc[VMIN] = 0;

	if ( ioctl( 0, TCSETA, &kbd ) < 0 ) perror( "ioctl" );

	while ( read ( 0, &buff, 1 ) == 0 ) printf( "looping\n" );

	printf("received %c\n", buff );

	if ( ioctl( 0, TCSETA, &kbd_save ) < 0 ) perror( "ioctl" );
}
------------------------ end program ---------------------------------------



More information about the Comp.sys.sgi mailing list