简体   繁体   中英

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. 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

#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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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