Are there any KeyUp/KeyDown type functions in GLUT?

I’m new to OpenGL (just started using a week ago at university). So far I’ve managed to get a cube up and can rotate it using keypresses, however it’s only using the glutKeyboardFunc() command. Is there a way to implement a KeyUp/KeyDown type function which will allow the cube to rotate when the key is pressed and then stop moving when the key is released. This would be without using Windows messages as I have to use it under Linux at university.

Any help would be appreciated.

Note: posted this before but the name screwed it up somehow :stuck_out_tongue:

keep track of whether a key is up or down yourself
eg pseudo code
glutkeydown()
{
key[keynum]=PUSHED;
{

glutkeyup()
{
key[keynum]=NOTPUSHED;
{

loop()
{
if (key[X]==PUSHED) blah
}

Don’t use glut’s functions ! They’re unable to understand that one might press two keys at the same time. If you’re under win32,
just use GetAsyncKeyState !

Morglum

But the answer is yes
glutKeyboardUp

OK thanks, but I still can’t get this to run.

I’m trying: glutKeyboardUp(asciiKeyUp); assuming that it’s the same as the keypress function: glutKeyboardFunc(asciiKey);

Sorry if I’m way off but I’m still new to this.

void keyboard_up(unsigned char key, int x, int y);

glutKeyboardUpFunc(keyboard_up);

Thanks heaps for the help. Everything’s running fine now

>>Don’t use glut’s functions ! They’re unable to understand that one might press two keys at the same time.<<

?? yes glut can understand if u push 2 or more keys at the same time

Do the experience by yourself if you don’t believe me ! LGLUT uses standard windows message (in its windows implementation). The same messages that are used for any text typing under windows. Click on “post reply”, go to any text-edit box, like “Your Reply”, and maintain 2 keys pressed. Here’s what I obtain :

hjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj

you see, only one key is repeated !
To work around this problem, you have to use another way of reading keyboard input. GetAsyncKeyState is one.

Morglum

do a demo in glut (as thats the api in question) yourself + see if it can recoginize 2 keypresses, u will find it can

>>hjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj<<

in answer to your question both h + j were printed, check my first post above + u will see that this confirms that the standard windows messages as well as glut can handle 2 key presses at once. u disproved yourself
btw u can dusable keyrepeats in glut (check the man pages for the command)

Morglum,

Your example is what is shown in a text editor, in other words how the text editor interprets the Windows events -> not relevant!

The GLFW api also uses standard Windows events to track key up/down events. Once a key down event is received, it is recorded in a keyboard table. Checking for the key with glfwGetKey() returns true until a key up event is received, regardless if any other keys were pressed in between. Also, checking for the value of a C/C++ array element is probably much faster than using a Windows function.

OOoook I’ve understood : so far, I had not made use of glutKeyboardUp. I should have read Gavin’s post more carefully !

Morglum

i want add code key up dwubn right left
in this project
#include <GL/glut.h>

GLfloat tx = 0.0;

void init(void)

{

glClearColor(1.0,1.0,1.0,0.0); // set the display window color to white

glMatrixMode(GL_PROJECTION); // set the projection parameters

gluOrtho2D(0.0, 1000.0, 0.0, 500.0);

}

void move(void)

{

if (tx > 1100.0)

{

tx = -800.0;

}

tx = tx + 0.2;

glutPostRedisplay();

}

void display(void)

{

glClear(GL_COLOR_BUFFER_BIT); // clear the display window

glPushMatrix();

glTranslatef(-tx, 0.0, 0.0);

glColor3f(0.0,0.0,1.0); //set line segment color to red

glBegin(GL_LINES);

glVertex2i(50,300); // specify the line segment geometry

glVertex2i(100,350);

glVertex2i(100,350);

glVertex2i(150,300);

glVertex2i(150,300);

glVertex2i(50,200);

glVertex2i(50,200);

glVertex2i(150,200);

// number 2

glVertex2i(200,275);

glVertex2i(250,200);

glVertex2i(250,200);

glVertex2i(300,275);

glVertex2i(300,275);

glVertex2i(250,350);

glVertex2i(250,350);

glVertex2i(200,275);

// number 0

glVertex2i(350,350);

glVertex2i(350,200);

// number 1

glVertex2i(400,350);

glVertex2i(500,350);

glVertex2i(500,350);

glVertex2i(400,200);

// number 7

glVertex2i(550,350);

glVertex2i(550,200);

// number 1

glVertex2i(600,350);

glVertex2i(600,200);

// number 1

glVertex2i(650,350);

glVertex2i(650,200);

// number 1

glVertex2i(700,275);

glVertex2i(750,200);

glVertex2i(750,200);

glVertex2i(800,275);

glVertex2i(800,275);

glVertex2i(750,350);

glVertex2i(750,350);

glVertex2i(700,275);

// number 0

glVertex2i(850,350);

glVertex2i(950,350);

glVertex2i(950,350);

glVertex2i(850,200);

// number 7

glEnd();

glPopMatrix();

glFlush(); // process all OpenGL functions as quickly as possible

}

int main(int argc, char** argv)

{

glutInit(&argc, argv); // initialize glut

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //set display mode

glutInitWindowPosition(100,100); //set top left display window position

glutInitWindowSize(800,600); //set display window height and width

glutCreateWindow(“An Example OpenGL Program”); //create display window

init(); // execute initialization procedure

glutDisplayFunc(display); //send graphics to display window

glutIdleFunc(move);

glutMainLoop(); // display everything and wait

}

i want add code (key up dwon right left)
in this project
#include <GL/glut.h>

GLfloat tx = 0.0;

void init(void)

{

glClearColor(1.0,1.0,1.0,0.0); // set the display window color to white

glMatrixMode(GL_PROJECTION); // set the projection parameters

gluOrtho2D(0.0, 1000.0, 0.0, 500.0);

}

void move(void)

{

if (tx > 1100.0)

{

tx = -800.0;

}

tx = tx + 0.2;

glutPostRedisplay();

}

void display(void)

{

glClear(GL_COLOR_BUFFER_BIT); // clear the display window

glPushMatrix();

glTranslatef(-tx, 0.0, 0.0);

glColor3f(0.0,0.0,1.0); //set line segment color to red

glBegin(GL_LINES);

glVertex2i(50,300); // specify the line segment geometry

glVertex2i(100,350);

glVertex2i(100,350);

glVertex2i(150,300);

glVertex2i(150,300);

glVertex2i(50,200);

glVertex2i(50,200);

glVertex2i(150,200);

// number 2

glVertex2i(200,275);

glVertex2i(250,200);

glVertex2i(250,200);

glVertex2i(300,275);

glVertex2i(300,275);

glVertex2i(250,350);

glVertex2i(250,350);

glVertex2i(200,275);

// number 0

glVertex2i(350,350);

glVertex2i(350,200);

// number 1

glVertex2i(400,350);

glVertex2i(500,350);

glVertex2i(500,350);

glVertex2i(400,200);

// number 7

glVertex2i(550,350);

glVertex2i(550,200);

// number 1

glVertex2i(600,350);

glVertex2i(600,200);

// number 1

glVertex2i(650,350);

glVertex2i(650,200);

// number 1

glVertex2i(700,275);

glVertex2i(750,200);

glVertex2i(750,200);

glVertex2i(800,275);

glVertex2i(800,275);

glVertex2i(750,350);

glVertex2i(750,350);

glVertex2i(700,275);

// number 0

glVertex2i(850,350);

glVertex2i(950,350);

glVertex2i(950,350);

glVertex2i(850,200);

// number 7

glEnd();

glPopMatrix();

glFlush(); // process all OpenGL functions as quickly as possible

}

int main(int argc, char** argv)

{

glutInit(&argc, argv); // initialize glut

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //set display mode

glutInitWindowPosition(100,100); //set top left display window position

glutInitWindowSize(800,600); //set display window height and width

glutCreateWindow(“An Example OpenGL Program”); //create display window

init(); // execute initialization procedure

glutDisplayFunc(display); //send graphics to display window

glutIdleFunc(move);

glutMainLoop(); // display everything and wait

}