Re: [cc65] cc65/C questions

From: Shawn Jefferson <jefferson_shawn_a8bit1yahoo.com>
Date: 2006-09-09 05:32:19
Here's a routine I use in some of my project, albeit
for the atari 8-bit.  You may have to change it
slightly for the c64.

/* text_input
 *
 * Get some text input from the user.  Pass in the max
number of characters
 * to be accepted, and a pointer to a string that is
at least as big plus one
 * for the null terminator to hold the string.
 *
 * Rudimentary editing is supported (backspace,
ctrl-arrows) when a non-empty
 * string is passed in.
 *
 * Returns the number of characters entered if
everything went ok
 * and the user pressed return, 0 if the user pressed
Escape.  Also the first character of the string is set
to 27 to denote the user pressed Escape.
 */
unsigned char __fastcall__ text_input(unsigned char x,
unsigned char y, char *text, unsigned char max)
{
  char c;
  unsigned char i;


  //--- Initialize ---//
  i = 0;
  cursor(1);
  cputsxy(x, y, text);                  // show text
  gotoxy(x, y); 

  while (1) {
    c = cgetc();

    switch(c) {
      case 27:                          // user
pressed escape
	text[0] = 27;                   // flag that user
pressed escape 
	cursor(0);                      // since a blank
string and escape 
	return(0);                      // both have a return
value of zero
	break;

      case 155:                         // user
pressed return
	text[strlen(text)] = '\0';
	cursor(0);
	return(strlen(text));
	break;

      case 126:                         // user
pressed backspace 
	if (i != 0) {
	  --i;
	  if (i == (strlen(text)-1))
	    text[i] = '\0';
	  else
	    text[i] = ' ';
	  //cclearxy(x+i, y, 1);
	  cputcxy(x+i, y, ' ');
	  gotoxy(x+i, y);
	}
	break;

      case 30:                          // user
pressed left arrow
	if (i != 0) {
	  --i;
	  gotoxy(x+i, y);
	}
	break;

      case 31:                          // user
pressed right arrow
	if (i < strlen(text)) {
	  ++i;
	  gotoxy(x+i, y); 
	}

      default:
	if (i == max) continue;                     // maxed
out

	if (isprint(c)) {                           // is a
printable char
	  text[i] = c;
	  cputc(c);
	  ++i;
	}
	break;
    }
  }
  // end of function
}

Shawn

--- Tom Watt <tom@awhost.biz> wrote:

> 2. I'm using the following function to get text at a
> prompt.  It works fine
> except if you backspace too much the cursor keeps
> going and will go up to
> the next line.  Is there an easy way to fix that?


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Sat Sep 9 05:32:28 2006

This archive was generated by hypermail 2.1.8 : 2006-09-09 05:32:31 CEST