c++ - OpenGL 和 GLUT 键盘功能

标签 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;
}

最佳答案

GLUT 无法检测到按下CtrlCtrl 的“枚举器”不是 GLUT_KEY_CTRL,而是 GLUT_ACTIVE_CTRL,这一事实也暗示了这一事实。 p>

但是,您可以查询按下另一个键时 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()了解更多详情。

关于c++ - OpenGL 和 GLUT 键盘功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21635367/

相关文章:

c++ - 如何限制用户输入的时间?

c++ - 在关闭文件时获取段错误

c++ - 如何在类周围共享参数 - C++

android - 是否应该动态加载和卸载 OpenGL 纹理

opengl - 定义指向 OpenGL 顶点数组对象的指针

c++ - OpenGl 无法正确呈现 .obj 文件

c++ - 在 Ubuntu 终端中编译时 OpenGL 引用的问题

c - 在 OpenGL 中设置 3D 空间的尺寸

C++模板函数类型推导

c - 在 Linux + Intel 驱动程序上启用合成时,OpenGL 应用程序会产生奇怪的效果