/* kbcolors.c Jeff Ondich, 4/12/04 Updated 5/24/10 This program demonstrates the detection of keystrokes. It depends on kbhit.c. To compile: gcc -Wall -o kbcolors kbcolors.c kbhit.c */ #include #include #include "kbhit.h" int main() { int k; int n = 100000; int count = 0; char *color = "Red"; char ch = '\0'; char newChar; init_keyboard(); fprintf( stderr, "Press 'q' to quit, 'g' for green, 'b' for blue, etc.\n" ); while( ch != 'q' ) { /* Print the current color and counter. */ fprintf( stderr, "%s: %d\n", color, count ); fflush( stderr ); count++; /* Delay for a while, checking for keystrokes while you wait. */ for( k=0; k < n; k++ ) { if( kbhit() ) { newChar = readch(); switch( tolower(newChar) ) { case 'r': color = "Red"; break; case 'g': color = "Green"; break; case 'b': color = "Blue"; break; case 'y': color = "Yellow"; break; case 'p': color = "Puce"; break; case 'm': color = "Mauve"; break; } if( newChar != ch ) { ch = newChar; break; } } } } close_keyboard(); return 0; }