简体   繁体   English

键盘输入无效,单击鼠标时有效

[英]Keyboard input doesn't work, works when clicking the mouse

I have this code: 我有以下代码:

#include <iostream>
#include <GL/glut.h>

using namespace std;
bool* Keys = new bool[256];

void keyboardDown(unsigned char key, int x, int y) 
{
    Keys[key] = true;
}

void keyboardUp(unsigned char key, int x, int y) 
{
    Keys[key] = false;
}

void reshape(int width, int height)
{
    GLfloat fieldOfView = 90.0f;
    glViewport(0, 0, (GLsizei)width, (GLsizei)height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(fieldOfView, (GLfloat)width / (GLfloat)height, 0.1, 500.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void draw() 
{
    if (Keys['e'])
        cout << "e" << endl;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glBegin(GL_QUADS);
    glColor3f(0.1, 0.1, 0.1); glVertex3f(1.0, 1.0, -1.0);
    glColor3f(0.1, 0.9, 0.1); glVertex3f(1.0, -1.0, -1.0);
    glColor3f(0.9, 0.1, 0.1); glVertex3f(-1.0, -1.0, -1.0);
    glColor3f(0.1, 0.1, 0.9); glVertex3f(-1.0, 1.0, -1.0);
    glEnd();
    glFlush();
    glutSwapBuffers();
}

void initGL(int width, int height)
{
    reshape(width, height);
    glClearColor(0.1f, 0.5f, 0.7f, 1.0f);
    glClearDepth(1.0f);
    glOrtho(0, 1, 0, 1, 1, 10);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
}

/* initialize GLUT settings, register callbacks, enter main loop */
int main(int argc, char** argv) 
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
    glutInitWindowSize(800, 800);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Perspective's GLUT Template");
    glutKeyboardFunc(keyboardDown);
    glutKeyboardUpFunc(keyboardUp);
    glutSpecialFunc(keyboardSpecialDown);
    glutSpecialUpFunc(keyboardSpecialUp);
    glutReshapeFunc(reshape);
    glutDisplayFunc(draw);
    glutIgnoreKeyRepeat(true); // ignore keys held down
    initGL(800, 600);
    glutMainLoop();
    return 0;
}

When I start the program, it writes 'e' only when I press on the mouse. 当我启动程序时,只有当我按下鼠标时它才会写“ e”。 I can't find the problem. 我找不到问题。 Why does it only work when the mouse is held down? 为什么只在按住鼠标时才起作用?

You dont have mouse functions in your code, thats why it is not working. 您的代码中没有鼠标功能,这就是为什么它不起作用。 Insert from here : http://www.zeuscmd.com/tutorials/glut/03-MouseInput.php 从此处插入: http : //www.zeuscmd.com/tutorials/glut/03-MouseInput.php

#include <iostream>
#include <gl/glut.h>

using namespace std;

bool lbuttonDown = false;

bool init()
{
    return true;
}

void display()
{

}

void mouse(int button, int state, int x, int y)
{
    if (button == GLUT_RIGHT_BUTTON)
    {
        if (state == GLUT_DOWN)
            cout << "Right button pressed"
            << endl;
        else
            cout << "Right button lifted "
            << "at (" << x << "," << y
            << ")" << endl;
    }
    else if (button == GLUT_LEFT_BUTTON)
    {
        if (state == GLUT_DOWN)
            lbuttonDown = true;
        else
            lbuttonDown = false;
    }
}

void motion(int x, int y)
{
    if (lbuttonDown)
        cout << "Mouse dragged with left button at "
        << "(" << x << "," << y << ")" << endl;
}

void motionPassive(int x, int y)
{
    cout << "Mouse moved at "
        << "(" << x << "," << y << ")" << endl;
}

void entry(int state)
{
    if (state == GLUT_ENTERED)
        cout << "Mouse Entered" << endl;
    else
        cout << "Mouse Left" << endl;
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);

    glutInitWindowPosition(200, 200);
    glutInitWindowSize(200, 200);

    glutCreateWindow("03 - Mouse Input");

    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutPassiveMotionFunc(motionPassive);
    glutEntryFunc(entry);

    if (!init())
        return 1;

    glutMainLoop();

    return 0;
}

I had the same problem [using freeglut] and calling 'draw()' [or whatever display callback you use] right after glutPostRedisplay() in the key callback function forced it to do the redraw. 我在键回调函数中的glutPostRedisplay()之后迫使它执行重绘时,遇到了相同的问题[使用freeglut]并调用'draw()'[或您使用的任何显示回调]。 I actually don't know why it behaves like this. 我实际上不知道为什么会这样。 After data is changed by keyboard input and the key callback returned, a redraw is expected but not executed. 在通过键盘输入更改了数据并返回了键回调之后,期望重绘但不执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM