Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!bloom-beacon!apple!bionet!agate!ucbvax!ucsd!telesoft!jjh From: jjh@telesoft.UUCP (Jim Hayes @dance) Newsgroups: comp.unix.wizards Subject: RESULTS: How to write keypressed()? Message-ID: <317@telesoft.UUCP> Date: 4 Oct 88 18:09:42 GMT Organization: TeleSoft Inc., San Diego, CA Lines: 47 My thanks to everyone who responded to my request for suggestions on testing for available characters in the keyboard buffer. After much experimentation, I finally got a function that seems to work: /*============================================================================*/ jmp_buf program_state; timeout_handler() { longjmp(program_state, 1); } short keypressed() { /* Returns 1 if there's a character in the keyboard input buffer, 0 otherwise */ int a_char; if ( isatty( fileno(stdin) ) == 1 ) { signal(SIGALRM, timeout_handler); if ( setjmp(program_state) == 1 ) { return (0); } else { alarm(1); a_char = getc(stdin); alarm(0); ungetc(a_char, stdin); return (1); } } else { return (0); } } /*============================================================================*/ My special thanks to David Barto for the basic algorithm, and to everyone who pointed out that I shouldn't be mixing read() and ungetc(). --Jim ------------------------------------------------------------------------------ "'Happiness' is being famous for your financial | ability to indulge in every kind of excess." | Jim Hayes "I suppose that's *one* way to define it." | "The part I think I'd like best is crushing | ...!ucsd!telesoft!jjh people who get in my way." |