From mboxrd@z Thu Jan 1 00:00:00 1970 From: Joachim To: "gcc@gcc.gnu.org" Subject: Newbie:getch?? Date: Sun, 20 Feb 2000 07:52:00 -0000 Message-id: <38B00E12.2C8022B5@singnet.com.sg> X-SW-Source: 2000-02/msg00578.html Hi there! I'm very new with gcc so please forgive me my stupid question! I was wondering if there would be a function available that reads only one character from stdin. To be more specific I need something that works the same way as: kbhit() or... getch() or... getche() alias Borlands I need it to write an input function to handle my own keyboard definitions. Unfortunatly getchar() is unsuitablee for what I want to do. Any glues on what to use? Thank's Joachim! /* 19. Feb. 2000 Joachim Bauernberger This is a list of features to be implemented for the Gas Tracker project. Here are a couple of ideas for Gas Tracker that are floating around in my head and scream to be put to paper 8-) 1) Rewrite the input function in the following way: --> check for keys pressed and accept the following: a) digits b) . use with float c) Y = YES d) N = NO e) H = HELP f) S = SAVE g) Q = QUIT h) C = Calculate i) arrow keys Note: * In case of a yes or no answer we have to be able to accept toupper(y/n) * The H key enables us to give guidance for lost souls 8-) * The S key saves everything to a file. * The Q key quits the program. * The arrow keys help us in navigating through the different locations of the program. This way we can change some of the values that we don't like but have already entered earlier. We have to make sure that all calculation will start after the C butten is pusehed. * This will make extensive use of pointers YIKES! */ #include #include #define CR 0x0d //carriage return (enter key) #define ESC 0x1b //Escape key #define TAB 0x09 //Tab key #define LF 0x0a //Line feed #define BACKSPACE 0x08 //Backspace #define SAVE 0x13 //Ctrl-S for Save #define HELP 0x08 //Ctrl-H for Help #define QUIT 0x11 //Ctrl-Q for Quit #define NULL 0 //Empty character #define TRUE 1 #define FALSE 0 #define LENGTH 10 //size of the string int input(char *string, int length); int main() { char string[LENGTH]; printf("Enter the text:\n"); if (!input(string,LENGTH)) printf("You entered: %s!\n",string); else printf("\n***** CANCELED. *****"); return 0; } int input(char *string, int length) { int done = FALSE; int cancel = FALSE; int index = 0; char ch; string[0] = NULL; //initialize the buffer do { ch = getc(stdin); /*we need to change this to something that accepts only one char!*/ /*check to see whether the buffer is full*/ if (index == length) { switch (ch) { case CR: break; default: putchar('\a'); ch = NULL; break; } //end switch } //end if /*process the keyboard input*/ switch(ch) { case CR: //enter key putchar(ch); putchar(LF); string[index] = NULL; done = TRUE; //set variable to exit while after break break; //break out of switch case NULL: break; case BACKSPACE: if (index == 0) { //bounds checking if the index is 0 break; } else putchar(ch); putchar(' '); putchar(ch); index--; break; case ESC: done = TRUE; cancel = TRUE; break; default: putchar(ch); string[index] = ch; index++; break; } //end switch } while (!done); //end do-while return(cancel); } //end