我将GLFW3用于OpenGL3.3 +的窗口处理。一切正常,但是我不明白为什么每次我按A键两次都会打印“按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 );
}
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...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句