为什么我在GLFW3中得到2个回调 - OpenGL

时间:2014-09-25 10:30:38

标签: glfw opengl-3

我正在使用GLFW3来处理OpenGL3.3 +的窗口。一切都正常,但我不明白为什么它打印" A press"每当我按下A键时,两次。特别是我希望它只能打印1次按键A。

static void My_Key_Callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    if( key == 'A' )
    {
        std::cout<< "A pressed\n";
        return;
    }
}


int main( int argc, char ** argv )
{
    // --1-- Initialise GLFW
    if( glfwInit() == false )
    {
        std::cerr << "Failed to initialize GLFW3\nQuiting....\n";
        exit(1);
    }


    // --2-- Create Window
    glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL

    GLFWwindow * window=NULL;
    window = glfwCreateWindow( 400, 400, "hello", NULL, NULL );
    if( window == NULL )
    {
        std::cerr << "Failed to create glfw window\nQuiting....\n";
        glfwTerminate();
        exit(1);
    }

    // --3-- Make current context
    glfwMakeContextCurrent( window );


// 
// .
// . Normal OpenGL code goes here
// .
//

    // set call back
    glfwSetKeyCallback( window, My_Key_Callback);

    // --5-- Main loop
    glfwSetInputMode( window, GLFW_STICKY_KEYS, GL_TRUE );
    glClearColor(0,0,.4,0);



    do
    {


        ////// Some more OpenGL code here 

        glDrawArrays(GL_TRIANGLES, 0, 36); 


        // swap buffers
        glfwSwapBuffers( window );
        glfwPollEvents();
    }
    while( glfwWindowShouldClose(window)==false );



}

2 个答案:

答案 0 :(得分:5)

GLFW3中有一个键盘事件回调,它也处理GLFW_RELEASE个事件:

typedef void(* GLFWkeyfun)(GLFWwindow *, int, int, int, int)

第四个参数是actionGLFW_PRESS, GLFW_RELEASE or GLFW_REPEAT. 您可以忽略密钥释放事件,例如

if (action == GLFW_RELEASE)
    return;

// ... handle PRESS, REPEAT of a key...

答案 1 :(得分:1)

GLFW的设计方式,在每个事件中调用回调函数,例如按下键或释放键。要忽略密钥释放事件,请将代码包含在条件语句中,如&#34; if(action!= GLFW_RELEASE)&#34;:

void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {

    std::cout << "Key pressed, action! " << key << ", " << action << std::endl;

    if (action != GLFW_RELEASE) {       // avoid double updates

    switch (key) {
        case GLFW_KEY_W: camZ -= 0.5f; break;
        case GLFW_KEY_S: camZ += 0.5f; break;

        case GLFW_KEY_A: camX -= 0.5f; break;
        case GLFW_KEY_D: camX += 0.5f; break;

        case GLFW_KEY_Z: camY -= 0.5f; break;
        case GLFW_KEY_X: camY += 0.5f; break;

        case GLFW_KEY_E: yaw -= 0.5f; break;        // around y axis
        case GLFW_KEY_R: yaw += 0.5f; break;

        case GLFW_KEY_UP: pitch -= 0.5f; break;     // around x axis
        case GLFW_KEY_DOWN: pitch += 0.5f; break;

        case GLFW_KEY_LEFT: roll -= 0.5f; break;    // around z axis
        case GLFW_KEY_RIGHT: roll += 0.5f; break;

        case GLFW_KEY_SPACE:
            scaleAmount = 1.0; theta = 0.5;
            yaw = 0.0; pitch = 0.5; roll = 0.0;
            camX = 0.0; camY = 0.0; camZ = 0.0;
            break;
        }

        print(Model);
    }
}