0
\$\begingroup\$

I want to use W, A, S and D keys like GLUT_KEY_LEFT.

My Code:

if (key == GLUT_KEY_RIGHT)
    rotate_y += 5;
else if (key == GLUT_KEY_LEFT)
    rotate_y -= 5;
else if (key == GLUT_KEY_UP)
    rotate_x += 5;
else if (key == GLUT_KEY_DOWN)
    rotate_x -= 5;

else if (key == GLUT_KEY_HOME)
    X1 += 0.5;
else if (key == GLUT_KEY_END)
    Y1 += 0.5;
else if (key == GLUT_KEY_INSERT)
    Z1 += 0.5;
\$\endgroup\$
1
  • 1
    \$\begingroup\$ So, what's your question here? And is it answered by anything more then pointing at intellisense/autocomplete/docs and GLUT_KEY_A? \$\endgroup\$ – Elva Jul 24 '17 at 13:48
2
\$\begingroup\$

glutSpecialFunc is used to register a callback handler for special keys; as it's documentation notes:

The special keyboard callback is triggered when keyboard function or directional keys are pressed.

For all other non-special keys you should register a second callback using glutKeyboardFunc, then just test for 'w', 'a', 's', 'd', etc in your code.

Again, from the documentation:

glutKeyboardFunc sets the keyboard callback for the current window. When a user types into the window, each key press generating an ASCII character will generate a keyboard callback. The key callback parameter is the generated ASCII character. The state of modifier keys such as Shift cannot be determined directly; their only effect will be on the returned ASCII data. The x and y callback parameters indicate the mouse location in window relative coordinates when the key was pressed. When a new window is created, no keyboard callback is initially registered, and ASCII key strokes in the window are ignored. Passing NULL to glutKeyboardFunc disables the generation of keyboard callbacks.

During a keyboard callback, glutGetModifiers may be called to determine the state of modifier keys when the keystroke generating the callback occurred.

Also, see glutSpecialFunc for a means to detect non-ASCII key strokes.

So:

glutSpecialFunc (SpecialKeyHandler);
glutKeyboardFunc (NormalKeyHandler);

And:

void SpecialKeyHandler (int key, int x, int y)
{
    if (key == GLUT_KEY_RIGHT)
        ; // do stuff
    if (key == GLUT_KEY_LEFT)
        ; // do other stuff
    // and so on...
}

And:

void NormalKeyHandler (unsigned char key, int x, int y)
{
    if (key == 'w')
        ; // do stuff
    else if (key == 'a')
        ; // do other stuff
    // and so on...
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.