View Full Version : Keyboard input trouble

03-18-2003, 04:27 PM
Hi I'm very new to open gl and am a bit stuck at the moment.

I've got a basic polygon which i'v gotten to rotate. But I've come to a stop with keyboard input.
Thing is I want my polygon to rotate right when I press the right key and left when I press the left key.

I've implemented the basic keyboard input commands and have put my keyboard methods and a switch statement after my display method (not in it).
my program compiles just fine but nothing happens!!!

I was just wondering if
a) I'v put my key input code in the wrong place
b) I'm using the wrong commands to get my polygons to rotate the other way (i.e gl.glRotated(Rotate, 0,0,-1); )

public void keyPressed(KeyEvent e) {}

public void keyTyped(KeyEvent e) {}

public void keyReleased(KeyEvent e) {

switch (e.getKeyCode()) {

case KeyEvent.VK_RIGHT:

gl.glRotated(Rotate, 0,0,1);


case KeyEvent.VK_LEFT:
gl.glRotated(Rotate, 0,0,-1);



I'm programing in java btw
Any help would be very much appreciated Thanks http://www.opengl.org/discussion_boards/ubb/smile.gif

03-18-2003, 06:53 PM
You probably want to start the rotating on a key press and stop on a release. You also need to redraw the screen. For animation is some kind of idle function that rotates and redraws required.

03-18-2003, 08:09 PM
u need a glut call back function like ,

void idle (void)
int i,j; // just to give an example
//increment the "i" as u press right key
//increment the "j" as u press left key


use i & j as ur subscripts for rotation in display function.....
something like this.........

void Display (void)
glRotatef(i or j,0,1,0);

// "1" is the axis u want to ROTATE UR POLYGON
// i or j is the value u want to rotate about



ur main shoud look some thing like this.

main (int argc, char **argv)
glutInit (&argc, argv);
glutInitWindowSize (w,h);
glutInitWindowPosition (150,10);
pw = glutCreateWindow ("Multi Function Display");
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
init ();

glutDisplayFunc (display);
glutIdleFunc (idle);
glutReshapeFunc (reshape);

glutMainLoop ();
return 0;


03-19-2003, 06:53 AM
the problem with your code is that you modify the modelview matrix inside your key event processing, and after that I'm quite sure you call glLoadIdenty, which will reset your transformation stack http://www.opengl.org/discussion_boards/ubb/wink.gif

do as prashantgp says, with a glut key callback and it should work.

have fun!