void TEXTBOX::EditText(float fTBF){
if (Text.size()==0){
string tmp;
Text.push_back(tmp);
ViewLocX=ViewLocY=CurLocX=CurLocY=0;
BottomScroll=RightScroll=false;
}
if (CurLocY>=int(Text.size())) CurLocY=int(Text.size())-1;
if (CurLocY<0) CurLocY=0;
if (CurLocX>int(Text[CurLocY].length())) CurLocX=int(Text[CurLocY].length());
//edit the text field
bool TakeInput=true;
bool CheckViewLoc=false;
string buff;
if (dbKeyState(VK_LEFT)==1){//left key
if ((ArrowKeyTime>1000)||(ArrowKeyTime==0)){
if (ArrowKeyTime>1000) ArrowKeyTime-=100;
CheckViewLoc=true;
CurLocX--;
if (CurLocX<0){
if (CurLocY==0) CurLocX=0;
else{//move to end of the line above
CurLocY--;
CurLocX=Text[CurLocY].length();
}
}
}
ArrowKeyTime+=int(fTBF);
TakeInput=false;
}
if (dbKeyState(VK_RIGHT)==1){//right key
if ((ArrowKeyTime>1000)||(ArrowKeyTime==0)){
if (ArrowKeyTime>1000) ArrowKeyTime-=100;
CheckViewLoc=true;
CurLocX++;
if (CurLocX>int(Text[CurLocY].length())){
if (CurLocY+1==int(Text.size())) CurLocX=Text[CurLocY].length();
else{//move to front of the line below
CurLocY++;
CurLocX=0;
}
}
}
ArrowKeyTime+=int(fTBF);
TakeInput=false;
}
if (dbKeyState(VK_UP)==1){//up key
if ((ArrowKeyTime>1000)||(ArrowKeyTime==0)){
if (ArrowKeyTime>1000) ArrowKeyTime-=100;
CheckViewLoc=true;
CurLocY--;
if (CurLocY<0) CurLocY=0;
else{
if (CurLocX>int(Text[CurLocY].length())) CurLocX=Text[CurLocY].length();
}
}
ArrowKeyTime+=int(fTBF);
TakeInput=false;
}
if (dbKeyState(VK_DOWN)==1){//down key
if ((ArrowKeyTime>1000)||(ArrowKeyTime==0)){
if (ArrowKeyTime>1000) ArrowKeyTime-=100;
CheckViewLoc=true;
CurLocY++;
if (CurLocY>=int(Text.size())) CurLocY=int(Text.size())-1;
else{
if (CurLocX>int(Text[CurLocY].length())) CurLocX=Text[CurLocY].length();
}
}
ArrowKeyTime+=int(fTBF);
TakeInput=false;
}
if (dbKeyState(VK_DELETE)==1){//delete key
if ((ArrowKeyTime>1000)||(ArrowKeyTime==0)){
if (ArrowKeyTime>1000) ArrowKeyTime-=100;
CheckViewLoc=true;
if (CurLocX<int(Text[CurLocY].length())){//delete on the line
Text[CurLocY].erase(CurLocX,1);
UpdateDisplayString=true;
}
}
ArrowKeyTime+=int(fTBF);
TakeInput=false;
}
if (TakeInput) {//none of the arrow keys have been pressed
ArrowKeyTime=0;
}
buff=dbEntry();
if (buff.length()>0){
//intercept the special keys
if (dbKeyState(VK_RETURN)==1){//enter key
if (!SingleLine){
string tmp=RightString(Text[CurLocY],CurLocX);
if (Text.size()==CurLocY+1){
Text.push_back(tmp);
}
else Text.insert(Text.begin()+CurLocY+1,tmp);
Text[CurLocY]=LeftString(Text[CurLocY],CurLocX);
CurLocY++;
CurLocX=0;
}
TakeInput=false;
CheckViewLoc=true;
}
if (dbKeyState(VK_BACK)==1){//backspace
CurLocX--;
if (CurLocX<0){
if (CurLocY>0){
if (Text[CurLocY].length()==0) Text.erase(Text.begin()+CurLocY);
CurLocY--;
CurLocX=Text[CurLocY].length();
}
else CurLocX=0;
}
else{
Text[CurLocY].erase(CurLocX,1);
}
TakeInput=false;
CheckViewLoc=true;
}
if (TakeInput){
CheckViewLoc=true;
if ((CurLocX<MaxLength)||(MaxLength==-1)){
if (CurLocX<int(Text[CurLocY].length())){//inside the string
if (InsertKey) Text[CurLocY][CurLocX]=buff[0];//writes over any spot
else {
string tmp="";
tmp+=buff[0];
Text[CurLocY].insert(CurLocX,tmp);
}
}
else {//at the edge of the string
Text[CurLocY]+=buff[0];
}
CurLocX++;
}
}
UpdateDisplayString=true;
}
dbClearEntryBuffer ( );
if (CheckViewLoc){
if (CurLocX<ViewLocX){
ViewLocX=CurLocX;
UpdateDisplayString=true;
}
if (CurLocY<ViewLocY){
ViewLocY=CurLocY;
UpdateDisplayString=true;
}
if (CurLocX>ViewLocX+LineSizeX-1-int(RightScroll)*2){
ViewLocX=CurLocX-LineSizeX+1+int(RightScroll)*2;
UpdateDisplayString=true;
}
if (CurLocY>=ViewLocY+LinesToDisplay-int(BottomScroll)*2){
ViewLocY=CurLocY-LinesToDisplay+int(BottomScroll)*2;
UpdateDisplayString=true;
}
}
}
This function is a part of a larger program (GUI2). It illustrates how I use "dbEntry();" to get keyboard input as text.
The guts of it is:
string buff=dbEntry();
if (buff.length()>0){
Text+=buff[0];
}
dbClearEntryBuffer ( );
Note the line "Text+=buff[0];"? This is where you would keep unwanted "extra" characters out....
The fastest code is the code never written.