I ran into this issue with a project where I was trying to extensively check for errors and was (seemingly) erroneously getting an error when setting callbacks despite the callbacks seemingly being registered and getting called appropriately. Here’s a simple C program I made which reproduces the issue:
#include <GLFW/glfw3.h>
#include <stdio.h>
void handleKey(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (action == GLFW_PRESS) {
printf("Key pressed: %d\n", key);
} else if (action == GLFW_RELEASE) {
printf("Key released: %d\n", key);
}
}
int main() {
if (!glfwInit()) {
printf("Failed to initialize GLFW\n");
return 1;
}
GLFWwindow* window = glfwCreateWindow(800, 600, "Test", NULL, NULL);
if (window == NULL) {
printf("Failed to create window\n");
glfwTerminate();
return 1;
}
printf("Expected key handler: %p\n", handleKey);
printf("Handler set: %p\n", glfwSetKeyCallback(window, handleKey));
glfwMakeContextCurrent(window);
while (!glfwWindowShouldClose(window)) {
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwMakeContextCurrent(NULL);
glfwTerminate();
return 0;
}
Sample output:
Expected key handler: 00007FF665411000
Handler set: 0000000000000000
Key pressed: 65
Key pressed: 68
Key released: 68
Key released: 65
Key pressed: 83
Key pressed: 76
Key released: 76
Key released: 83
I’m using the GLFW 3.4 release on Windows and compiling with
cl /MD /IC:\glfw\include test.c C:\glfw\lib\glfw3.lib User32.Lib Gdi32.Lib Shell32.Lib