views:

185

answers:

2

I've found a lot of information about using GLUT to detect whether the control key is pressed using the GLUT_ACTIVE_CTRL macro. This macro only works, apparently, within a keyboard or mouse callback function. I need to know whether or not the control key is pressed at a point in my main loop, but GLUT_ACTIVE_CTRL doesn't seem to work in this context.

So, is there a way to detect key up and key down events on the control key (without any other keys being typed) in a platform independent OpenGL-ish way?

EDIT: The keyboard callback is not fired (at least for a default setup) when the control key is pressed. This is the basic problem, that I can only test whether the control key is or isn't pressed when another key is pressed and thus fires the keyboard callback.

My setup is something like:

// ... in main function:
glutKeyboardFunc(keyboard);

//later in the code: 

void keyboard(unsigned char key, int _x, int _y)
{
    printf("keydown \n");

    if (glutGetModifiers() == GLUT_ACTIVE_CTRL) {
            printf("control key is pressed.\n");
    }
    //etc.

When I press any normal character "keydown " is printed to stdout. When I press the ctrl key, nothing happens. If I press ctrl+c, "keydown control key is pressed." is printed.

However, in my main loop I added:

if (glutGetModifiers() == GLUT_ACTIVE_CTRL) {
    printf("Control key down.\n");
} else {
    printf("Control key up.\n");
}

and it always prints "Control key up." regardless of whether I am pressing the control key or not.

A: 

In the event handler record the setting of the key. You can then expose that storage to be read from your main loop.

Jay
That will not work with GLUT using the typical methods (glutKeyboardFunc(callback)) because the keyboard callbacks are not fired for modifier keys.
quadelirus
At least, they aren't fired in the default set-up.
quadelirus
+1  A: 

Not using GLUT as specced. You may wish to check out GLFW.

genpfault