PDA

View Full Version : Getting textual input



Richa
09-30-2002, 11:15 PM
Hi,
i want to take textual input in my window, rendered using glut. i have created a rectangle depicting a text box and have set it using glRasterPos2i(), so that the text gets entered in it. i have used glutBitmapCharacter(GLUT_BITMAP_9_BY_15,k); where k is the character according to the keypress. this is all working fine.... my trouble is i am not able to handle the backspace.... i am able to go back one character but the character already present there is not "erased", the new keypress is written on top of the old one.....
how can i erase the previous character??????

my keyboard function is as follows:

void keyboard(unsigned char k,int x,int y)
{
glColor3f(0.0,0.0,0.0);
glRasterPos2f(rx,2.27);

if((inc==0)&&((int)k==13))
{
glutSetWindow(id1);
glutPostRedisplay();
}
else if(int(k)!=13)// keypressed is not the return key
{
if(int(k)==8)//key press is backspace
{
inc--;
rx-=0.1;
glColor3f(1.0,1.0,1.0);
glutBitmapCharacter(GLUT_BITMAP_9_BY_15,s[inc]);
glColor3f(0.0,0.0,0.0);

}
else
{
s[inc++]=k;
glutBitmapCharacter(GLUT_BITMAP_9_BY_15,k);
rx+=0.1;
glutSwapBuffers();
glutSwapBuffers();
}

}
else
{
s[inc]='\0';
if(!recogniz())
{
glutSetWindow(id);
glutPostRedisplay();
}

glutSetWindow(id1);
glutPostRedisplay();
rx=-2.5;
inc=0;
}


}


[This message has been edited by Richa (edited 10-01-2002).]

[This message has been edited by Richa (edited 10-01-2002).]

mikael_aronsson
10-01-2002, 03:08 AM
Hi !

Why does everybody put two swapbuffers after each other..... ?

Anyway, you can't do it like that, you have to save the key strokes in a buffer and call redisplay, then you have to put code in your display callback function to draw the characters.

Mikael

nexusone
10-02-2002, 08:10 AM
The problem is the way you are drawning the text on the screen.
It is a common error to put graphics routines in your keyboard routine, it works for simple examples, but not good as you start to get a complex program.

first create a variable called

char key_input[30]; // storage for 30 charactors, if you need to have more then 30 just increase the size of the array.
int key_size; // track how may keystrokes.

void keyboard(unsigned char k,int x,int y)
{

if ( k > 31 ) // Check for a charactor not a control key
{
key_input[key_size] = k;
key_size++; // increase our number of charactors stored.
}

if ( k == 8 )
{
key_size--; //subtract key stroke
key_input[key_size] = 0; //erase charactor
}

}

Display()
{
// note normal code left out here for example

glColor3f( 1.0, 1.0, 1.0); set text color to white
Sprint( x, y, key_input); x/y is where text will start.


glutSwapBuffer();// this should be the last command you call after drawing all your objects and the only place you put this command!!!! should be nowhere else in your code.
}

// my print routine...
void Sprint( int x, int y, char *st)
{
int l,i;

l=strlen( st );
glRasterPos2i( x, y);
for( i=0; i < l; i++)
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, st[i]);
}

}


[QUOTE]Originally posted by Richa:
[B]Hi,
i want to take textual input in my window, rendered using glut. i have created a rectangle depicting a text box and have set it using glRasterPos2i(), so that the text gets entered in it. i have used glutBitmapCharacter(GLUT_BITMAP_9_BY_15,k); where k is the character according to the keypress. this is all working fine.... my trouble is i am not able to handle the backspace.... i am able to go back one character but the character already present there is not "erased", the new keypress is written on top of the old one.....
how can i erase the previous character??????



[This message has been edited by nexusone (edited 10-02-2002).]