加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 多个键无效

发布时间:2020-12-16 10:01:34 所属栏目:百科 来源:网络整理
导读:我通过使用freeglut函数来解决密钥问题.所有的键都工作正常,除了ctrl alt D.我不知道为什么它不能正常工作我做错了. 这是代码: #includeiostream#includecstdlib#include GLfreeglut.husing namespace std;void Display(void) { glClear(GL_COLOR_BUFFER_B
我通过使用freeglut函数来解决密钥问题.所有的键都工作正常,除了ctrl alt D.我不知道为什么它不能正常工作我做错了.
这是代码:

#include<iostream>
#include<cstdlib>
#include <GLfreeglut.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 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);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-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;
}

解决方法

当您按下CTRL D键不等于’d’或’D’时它等于EOT传输结束并等于此键== 0x04(更多 info here)所以你需要添加到你的if表达式别的

if (key == 'd' || key == 'D' || key == 0x04)

接下来当您同时按下ALT和CTRL时,glutGetModifiers()将返回以下任何符号位掩码的组合:GLUT_ACTIVE_CTRL GLUT_ACTIVE_ALT …更多info here

如果按下按钮d的按钮可能如下所示:

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上适用于我.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读