Hi. I was trying to make a text editor using C++ graphics. I am using TC++.
For backspace, I move the cursor by 8 pixels(default character size) and print a rectangle(ascii 219) in the background color. The problem lies with the code for backspace when the cursor is at a new line. I also can't get the arrow keys to work. I used their scan codes and the switch is working fine, but instead of moving the cursor to the specified coordinates, it prints a rectangle and shifts the cursor by a few spaces. I have attached the code of the function. I am sorry if this seems like a random barrage of unindented garbage. I refrained from posting the rest, but do let me know if you have to see the rest for any reason.
Thanks.
The code:
struct charpos
{ char ch;
int x;
};
void textput()
{ charpos a[1000];
char b[2];
int x=10;//t=1;
b[1]='\0';
for (int i=0;i<10;i++)// 10 for now
{ showcursor();
b[0]=getch();
switch (b[0])
{ case 27 : {exit(0);}
case 13 : {moveto(0,x);
x+=10;
break;}
case 8 : { //moverel(-8,0); //BACKSPACE CODE...
int x1=getx(),y1=gety();
//int x1=getx();//cout<<x1;
if (x1==0) // checking if prev input was enter by x coordinate
{ moveto(a[i-1].x,gety()-10);
outtext("THE CURSOR IS HERE ");
itoa(a[i-1].x,b,10);//b[0]=(char)a[i-1].x;
outtext(b);
//moveto((a[i-1].x),(gety()-20));
break;} // Using stored x coordinate to shift to prev line's end
else { moveto(getx()-8,gety());
setcolor(WHITE);//background color
b[0]=219; //a[i-t];
outtext(b);
colorinit();
//t++;
moveto(getx()-8,gety());
break;
}
}
case 0: { b[0]=getch(); // trying to work with arrow keys
switch(b[0])
{ case 75: { //outtext("left");
moveto(getx()-24,gety());break;}
case 77: { //outtext("right");
moveto(getx()+8,gety());break;}
}
}
case 127 : { moveto(getx()+8,gety());
setcolor(WHITE);
b[0]=219;
outtext(b);
colorinit();
}
default : { if(b[0]!=8||b[0]!=0)
{a[i].ch=b[0];
a[i].x=getx();
outtext(b);break;}}
}
}
}