2014-02-07 75 views
2

如何註冊按下CTRL鍵?下面的代碼適用於除CTRL所有的鍵:OpenGL和GLUT鍵盤功能

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

回答

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()