RESULTS: How to write keypressed()?
Jim Hayes @dance
jjh at telesoft.UUCP
Wed Oct 5 04:09:42 AEST 1988
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." |
More information about the Comp.unix.wizards
mailing list