Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!snorkelwacker!bloom-beacon!eru!luth!sunic!mcsun!cernvax!chx400!sgzh!root From: root@sgzh.uucp (Bruno Pape) Newsgroups: comp.sys.sgi Subject: Re: How to do non-blocking keyboard input Keywords: non-blocking keyboard i/o help Message-ID: <1990Jun15.095038.4593@sgzh.uucp> Date: 15 Jun 90 09:50:38 GMT References: <14670@thorin.cs.unc.edu> Reply-To: root%sgzh.uucp@uunet.uu.net (Bruno Pape) Organization: Silicon Graphics S.A., Zuerich, Switzerland Lines: 44 In article <14670@thorin.cs.unc.edu> taylorr@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@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 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 ---------------------------------------