This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Newbie:getch??
- To: Joachim <nuklear at singnet dot com dot sg>
- Subject: Re: Newbie:getch??
- From: Jose Camargo <ildefonso_camargo at yahoo dot com>
- Date: Thu, 16 Mar 2000 09:44:46 -0600 (CST)
- Cc: gcc at gcc dot gnu dot org
Ok, depending on the plataform where you are working
on, you should use the ncurses library or something
like that. At least, that's the only way I know.
--- Joachim <nuklear@singnet.com.sg> escribió:
> 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 <conio.h>
>
> 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 <stdio.h>
> #include <ctype.h>
>
> #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
>
_________________________________________________________
Do You Yahoo!?
Obtenga su dirección de correo-e gratis @yahoo.com
en http://correo.espanol.yahoo.com