【发布时间】:2016-02-10 08:23:40
【问题描述】:
我在使用freeglut 函数时遇到了按键问题。除ctrl+alt+D 外,所有键都工作正常。我不知道为什么它不工作我做错了什么。
这是代码:
#include<iostream>
#include<cstdlib>
#include <GL\freeglut.h>
using namespace std;
void Display(void) {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnd();
glFlush();
}
void SpecialKeys(int key, int xpos, int ypos) {
if (key == GLUT_KEY_UP) {
cout << "up key press" << endl;
}
else if (key == GLUT_KEY_DOWN) {
cout << "down key press" << endl;
}
else if (key == GLUT_KEY_RIGHT) {
cout << "Right key press" << endl;
}
else if (key == GLUT_KEY_LEFT) {
cout << "left key press" << endl;
}
glutPostRedisplay();
}
void KeysFun(unsigned char key, int xpos, int ypos) {
if (key == 'a' || key == 'a') {
cout << "A Key press" << endl;
}
else if (key == 's' || key == 'S') {
int mod = glutGetModifiers();
if (mod == GLUT_ACTIVE_ALT) {
cout << "Alt+S press" << endl;
}
}
else if (key == 'd' || key == 'D') {
int mod = glutGetModifiers();
int mod2 = glutGetModifiers();
if (mod == GLUT_ACTIVE_CTRL && mod2== GLUT_ACTIVE_ALT) {
cout << "CTRL+Alt+D press" << endl;
}
}
glutPostRedisplay();
}
void init(void) {
glClearColor(0.0f,0.0f,0.0f,0.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
}
int main(int argc, char**argv) {
glutInit(&argc, argv);
glutCreateWindow("Frame");
init();
glutDisplayFunc(Display);
glutKeyboardFunc(KeysFun);
glutSpecialFunc(SpecialKeys);
glutMainLoop();
return EXIT_SUCCESS;
}
【问题讨论】: