Trouble with curses

draper at inmet.inmet.com draper at inmet.inmet.com
Mon Oct 29 04:21:00 AEST 1990


/* Written  1:38 pm  Oct 27, 1990 by ramsey at 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 <stdio.h>
#include <curses.h>
#include <term.h>

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 at 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 at 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 <stdio.h>
#include <curses.h>
/* #include <term.h>  I did not need this */
#include <ctype.h>  /* 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 at inmet.inmet.com
UUNET: uunet!inmet!draper
-----
Intermetrics Microsystems Software, Incorporated
733 Concord Avenue	   Cambridge,  MA  02138
(617) 661-0072 x4573



More information about the Comp.lang.c mailing list