问题描述
我通过使用 freeglut
函数面临键的问题。除了 ctrl + alt + D
,所有键都可以正常工作。我不知道为什么它不工作我做错了。
这是代码:
I am facing a problem with keys by using freeglut
functions. All keys are working fine except ctrl+alt+D
. I don't know why it's not working what I'm doing wrong.
This is Code:
#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;
}
推荐答案
键
不等于'd'或'D'
它等于EOT传输结束, code> key == 0x04 (更多)所以你需要添加到你的if表达式else
When you press CTRL+D key
doesn't equal 'd' or 'D'
it equals to EOT End of transmission and equal to this key == 0x04
(more info here) so you need add to your if expression else
if (key == 'd' || key == 'D' || key == 0x04)
下一步同时按ALT和CTRL glutGetModifiers()
将返回以下任何符号位掩码的组合:GLUT_ACTIVE_CTRL GLUT_ACTIVE_ALT ...更多
next when you press ALT and CTRL simultaneously glutGetModifiers()
will return you combination of any of the following symbolic bitmasks: GLUT_ACTIVE_CTRL GLUT_ACTIVE_ALT... more info here
因此,如果按钮d的按钮可能看起来像这样:
resulting if block for button d press may look like this:
else if (key == 'd' || key == 'D' || key == 0x04) {
int mod = glutGetModifiers();
if (mod == (GLUT_ACTIVE_CTRL|GLUT_ACTIVE_ALT)) {
cout << "CTRL+Alt+D press" << endl;
}
}
在Linux上适用于我。
Works for me on Linux.
这篇关于多个键不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!