View Full Version : GLUT function for detecting when key is held down
dellatorre 01-08-2010, 11:49 PM Is there a GLUT function for detecting when a key is held down? like when you hold down the up arrow key to go forward, instead of having to repeatedly press/depress it.
|
|
no but you can trick it.
glut has key pressed and key released(up) function. make a boolean variable for each key, set true when a key is pressed, set false when key is released.
dellatorre
01-09-2010, 08:38 PM
Ok, I have that boolean in my code now.
What I want to happen is that the movement continues when I hold down the up arrow key.
Here is snippets of my code:
glutIdleFunc(update);
glutSpecialFunc(specialChar);
glutSpecialUpFunc(keyBoardUp);
.
.
.
void update()
{
if (upKeyPressed) {
speed += 0.06f;
translate(0,0,speed);
display();
}
void specialChar( int key, int x, int y )
{
switch( key ) {
case GLUT_KEY_UP:
speed += 0.06f;
upKeyPressed = true;
break;
case GLUT_KEY_DOWN:
if ( speed > 0.01f )
speed -= 0.01f;
else
speed = 0;
break;
case GLUT_KEY_RIGHT:
rotate( 3, 0, 1, 0 );
break;
case GLUT_KEY_LEFT:
rotate( -3, 0, 1, 0 );
break;
}
display();
}
void keyBoardUp( int key, int x, int y )
{
switch( key ) {
case GLUT_KEY_UP:
speed = 0;
upKeyPressed = false;
break;
case GLUT_KEY_DOWN:
speed = 0;
break;
case GLUT_KEY_RIGHT:
speed = 0;
break;
case GLUT_KEY_LEFT:
speed = 0;
break;
}
display();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
redsquare();
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf( modelViewMatrix );
worldCube();
glFlush();
glutSwapBuffers();
}
void translate( GLfloat x, GLfloat y, GLfloat z )
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef( x, y, z );
glMultMatrixf( modelViewMatrix );
glGetFloatv( GL_MODELVIEW_MATRIX, modelViewMatrix );
}
But its not working. When I hold down the up arrow key it doesn't continue to move, I still have to keep pressing the key.
I tried replacing the "if" statement in my "update" function with a "while" statement, but that just gives me an infinite loop, and the movement doesn't stop when I release the up arrow key.
Any suggestions?
thanks :-))
im having trouble to read the code, probably try to fix it with spaces and stuff :D
but my quick advice is has you register the key up function to the glut, eg:
glutSpecialFunc(onSpecial);
glutSpecialUpFunc(onSpecialUp);
regards
aha sorry i missed the first three line. but i think i notice your error
display function on glut must be called repeatedly, not just when you press and release the key. there are two way of doing this:
register your display function to the glut, eg:
glutIdleFunc(onIdle); // you had this one
glutDisplayFunc(onDisplay); // but not this one
or you can call the display function on the idle eg:
void onIdle() {
//your code here
display();
}
after you did one of this, remove the display function in both of your specialChar and keyBoardUp function.
hope that helps
regards
dellatorre
01-09-2010, 09:07 PM
Yes I already have the glutDisplayFunc statement in my main method, and I have a call in my idle function (update) to my display method.
Here is snippets of my code, hopefully it displays more clearly this time:
bool upKeyPressed;
.
.
glutIdleFunc(update);
glutSpecialFunc(specialChar);
glutSpecialUpFunc(keyBoardUp);
.
.
.
void update()
{
if (upKeyPressed) {
speed += 0.06f;
translate(0,0,speed);
display();
}
}
.
.
void specialChar( int key, int x, int y )
{
switch( key ) {
case GLUT_KEY_UP:
speed += 0.06f;
upKeyPressed = true;
break;
case GLUT_KEY_DOWN:
if ( speed > 0.01f )
speed -= 0.01f;
else
speed = 0;
break;
case GLUT_KEY_RIGHT:
rotate( 3, 0, 1, 0 );
break;
case GLUT_KEY_LEFT:
rotate( -3, 0, 1, 0 );
break;
}
display();
}
.
.
void keyBoardUp( int key, int x, int y )
{
switch( key ) {
case GLUT_KEY_UP:
speed = 0;
upKeyPressed = false;
break;
case GLUT_KEY_DOWN:
speed = 0;
break;
case GLUT_KEY_RIGHT:
speed = 0;
break;
case GLUT_KEY_LEFT:
speed = 0;
break;
}
display();
}
.
.
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
redsquare();
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf( modelViewMatrix );
worldCube();
glFlush();
glutSwapBuffers();
}
.
.
void translate( GLfloat x, GLfloat y, GLfloat z )
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef( x, y, z );
glMultMatrixf( modelViewMatrix );
glGetFloatv( GL_MODELVIEW_MATRIX, modelViewMatrix );
}
dellatorre
01-09-2010, 09:12 PM
ooops sorry, I just read your last sentence....missed it the first time I read your post.
I'm trying it now - "remove the display function in both of your specialChar and keyBoardUp function."
I think it may be working now.
dellatorre
01-09-2010, 09:32 PM
I got it working now.
thanks so much :)
CGTalk Moderation
01-09-2010, 09:32 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
vBulletin v3.0.5, Copyright ©2000-2013, Jelsoft Enterprises Ltd.