OpenGL和GLUT键盘功能

时间:2014-02-07 18:40:56

标签: c++ opengl glut keyboard-events

如何注册CTRL键被按下?以下代码适用于除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;
}

1 个答案:

答案 0 :(得分:4)

GLUT无法检测只是 Ctrl Ctrl 的“枚举器”不是GLUT_ KEY _CTRL,而是GLUT_ ACTIVE _CTRL。

但是,当按下另一个键时,您可以查询Ctrl的状态:

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

有关详细信息,请参阅documentation of glutGetModifiers()