Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glfwSetKeyCallback() from GLFW is not called constantly during a key press

I'm trying to make my game do something when I press a key. I'm using GLFW to handle Windows API for me. I got stuck and confused when I used the mention callback function in title.

Referenced doc: https://www.glfw.org/docs/latest/input_guide.html#input_keyboard

I followed "Key input" resources (not "Text input"), but I get a kinda text input callback curve, like, when I keep pressed a key, I get pressed key event, then it stops for some time, and then I get repeated events. Shouldn't callbacks be constant in time (without that time gap)? This is my code:

void onKeyPress(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    if (key == GLFW_KEY_W && action != GLFW_RELEASE)
    {
        camera.pos += glm::vec3(0.0f, 0.0f, 1.0f);
        updateCameraMatrices();
    }
}

// ...

glfwSetKeyCallback(window, onKeyPress);
like image 262
Popescu Flaviu Avatar asked Sep 03 '25 17:09

Popescu Flaviu


2 Answers

Nvm. It seems like my expectations were wrong. I should just listen for GLFW_PRESS and GLFW_RELEASE, store the key status, and use if(status) in my main loop.

like image 80
Popescu Flaviu Avatar answered Sep 07 '25 20:09

Popescu Flaviu


There's no need to use event callbacks at all if you just want to know wether a key is pressed or not. Just use glfwGetKey(window,key) to get the current key status (this returns GLFW_PRESS or GLFW_RELEASE). See here for more details.

like image 23
peabrainiac Avatar answered Sep 07 '25 21:09

peabrainiac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!