4
\$\begingroup\$

Well, this sound ridiculous but the logic is escaping me. I have drawn my cube in OpenGL and I want to translate it to the right if any of the key on the keyboard is pressed. I know how to do this for specific keys but not for all the keys in one function? Do I have to code this for every key?

I was thinking to use glutSpecialFunc and glutKeyboardFunc? Is there any shorter way of doing this?

\$\endgroup\$
1
  • \$\begingroup\$ Reasoning for edit: OpenGL and GLUT are easy to confuse, but distinct. OpenGL is only a graphics API. GLUT is a toolkit that creates an OpenGL context inside a windowing system, and that's what's handling windows, key presses, mouse events, and such. \$\endgroup\$
    – Anko
    Oct 17, 2015 at 23:20

1 Answer 1

1
\$\begingroup\$

Using glutSpecialFunc and glutKeyboardfunction is correct way. Whenever a key is pressed one of the function register this.

For example:

void pressKey(unsigned char key, int x, int y){
if(key=='w') close program (for example);
else movecube;
}

int main(int argc, char **argv) {

    glutInit(&argc, argv);
    initGL();
    glutCreateWindow("WINDOW");
    glutSpecialFunc(pressKey);
    glutKeyboardFunc(pressKey);
    glutMainLoop();

    return 1;
}

Whenever a key is pressed pressKey function is called and cube is moved. Anyway this wont work for some keys (ALT, CTRL, SHIFT or CAPSLOCK).

\$\endgroup\$
1
  • \$\begingroup\$ Yeah, I figured it to be the way out \$\endgroup\$
    – CN1002
    Nov 22, 2015 at 13:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .