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);
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With