Question

How can I register that the CTRL key is depressed? The following code works for all keys except CTRL:

switch (key)
{
case GLUT_KEY_RIGHT:
    cout << "right key" << endl;
    glutPostRedisplay();  // Redraw the scene
    break;
case GLUT_KEY_LEFT:
    cout << "left key" << endl;
    glutPostRedisplay();  // Redraw the scene
    break;
case GLUT_KEY_UP:
    cout << "up key" << endl;
    glutPostRedisplay();  // Redraw the scene
    break;
case GLUT_KEY_DOWN:
    cout << "down key" << endl;
    glutPostRedisplay();  // Redraw the scene
    break;
case GLUT_ACTIVE_CTRL:
    cout << "CTRL pressed" << endl;
    glutPostRedisplay();  // Redraw the scene
    break;
}
Was it helpful?

Solution

GLUT cannot detect just the press of Ctrl. This fact is also hinted at by the fact that the "enumerator" for Ctrl is not GLUT_KEY_CTRL, but GLUT_ACTIVE_CTRL.

However, you can query the state of Ctrl when another key is pressed:

case GLUT_KEY_RIGHT:
    cout << "right key";
    if (glutGetModifiers() & GLUT_ACTIVE_CTRL)
        cout << " w/Ctrl";
    cout << endl;
    glutPostRedisplay();  // Redraw the scene
    break;

See the documentation of glutGetModifiers() for more details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top