Path: utzoo!attcan!uunet!bu.edu!inmet!draper From: draper@inmet.inmet.com Newsgroups: comp.lang.c Subject: Re: Trouble with curses Message-ID: <20900012@inmet> Date: 28 Oct 90 17:21:00 GMT References: <173826@<1990Oct27> Lines: 139 Nf-ID: #R:<1990Oct27:173826:inmet:20900012:000:3382 Nf-From: inmet.inmet.com!draper Oct 28 12:21:00 1990 /* Written 1:38 pm Oct 27, 1990 by ramsey@NCoast.ORG in inmet:comp.lang.c */ /* ---------- "Trouble with curses" ---------- */ Hi, this is Connie. I'm accessing this net with a friends account. I have a question about the curses package. Heres the skeletal code: #include #include #include main() { int ch; /* init curses package */ initscr(); nonl(); cbreak(); noecho(); for (;;) { ch = getch(); switch(ch) { case 27: /* escape */ do_escape(); break; [ ... irrelevant stuff deleted ... ] } } } do_escape() { switch(tolower(getch())) { case 'e': do_exit(); break; [ ... irrelevant stuff deleted ... ] } } Basically what is happening is the the user presses ESCAPE key follow by 'E' key to exit. But I have to press 'E' twice after pressing ESCAPE to get an exit. I can't figure out why. Does anybody know ? I would prefer a post to net but if you must send mail please do not send it to this box instead send it to me at aj398@CLEVELAND.FREENET.EDU , Thankyou. /* End of text from inmet:comp.lang.c */ I made a few adjustments to the code above and then compiled it on a Sun4. I got it to run without any problems. Below is a copy of the code after I modified it. It was compiled and linked using the following command: cc test.c -lcurses -ltermcap -o test The modifications I made had nothing to do with the routines used to read in characters. It almost sounds like when you press the 'E' key the first time it is not getting read in by curses until a subsequent keystroke causes the getch() in the switch to read the character out of the buffer. I would modify the code to get the character and then switch on the char. By doing this you can run it under a debugger and see if the call to getch() is actually getting a char when it is suppossed to. - Dave Internet: draper@inmet.inmet.com UUNET: uunet!inmet!draper ----- Intermetrics Microsystems Software, Incorporated 733 Concord Avenue Cambridge, MA 02138 (617) 661-0072 x4573 ------------------------------------------------------------------------ /* Modified code with comments */ #include #include /* #include I did not need this */ #include /* needed this for the isupper, tolower macros */ main() { int ch; /* init curses package */ initscr(); nonl(); cbreak(); noecho(); for (;;) { ch = getch(); switch(ch) { case 27: /* escape */ do_escape(); break; /* ... irrelevant stuff deleted ... */ } } } do_escape() { switch(tolower(getch())) { /* I would watch out on the above line of code. tolower is a */ /* macro that might have undesriable effects should the user */ /* enter a character that is already in lower case. A good */ /* thing to do would be check if the char was an upper case */ /* using "isupper" before calling "tolower". */ case 'e': do_exit(); /* added a definition and bosy for this function */ break; /* ... irrelevant stuff deleted ... */ } } /* clean up and exit */ do_exit() { endwin(); exit(); } /* end of modified code */ Internet: draper@inmet.inmet.com UUNET: uunet!inmet!draper ----- Intermetrics Microsystems Software, Incorporated 733 Concord Avenue Cambridge, MA 02138 (617) 661-0072 x4573