簡體   English   中英

鍵盤輸入無效,單擊鼠標時有效

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

我有以下代碼:

#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;
}

當我啟動程序時,只有當我按下鼠標時它才會寫“ e”。 我找不到問題。 為什么只在按住鼠標時才起作用?

您的代碼中沒有鼠標功能,這就是為什么它不起作用。 從此處插入: 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;
}

我在鍵回調函數中的glutPostRedisplay()之后迫使它執行重繪時,遇到了相同的問題[使用freeglut]並調用'draw()'[或您使用的任何顯示回調]。 我實際上不知道為什么會這樣。 在通過鍵盤輸入更改了數據並返回了鍵回調之后,期望重繪但不執行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM