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

mku*_*use 2 glfw opengl-3

我正在使用GLFW3来处理OpenGL3.3 +的窗口.一切正常,但是我不明白为什么每当我按下A键时它会打印"A press"两次.特别是我希望它只能打印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 );



}
Run Code Online (Sandbox Code Playgroud)

Bre*_*ale 5

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

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

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

if (action == GLFW_RELEASE)
    return;

// ... handle PRESS, REPEAT of a key...
Run Code Online (Sandbox Code Playgroud)