glfwPollEvents
Handles the keyboard, mouse and window events that arrived since the last call, then returns at once.
Signature
void glfwPollEvents(void);
How it works
The operating system puts events in a queue: a key was pressed, the mouse moved, the close button was clicked. Until you process that queue the window "does not respond", and glfwGetKey keeps returning old states.
glfwPollEvents empties the queue. Your callbacks (key, cursor, framebuffer size…) run INSIDE this call, one per event. Call it once per frame; it never waits, which suits a game or animation that redraws continuously.
Tips & pitfalls
Call it from the main thread only.
On Windows, dragging or resizing the window blocks inside this call until the mouse is released. Redraw from the framebuffer-size callback if the picture must follow the resize.
An editor or tool that only redraws on input should use
glfwWaitEventsand save the CPU.
Errors
When the call is invalid it does nothing and sets an error, which glGetError returns:
- GLFW_NOT_INITIALIZED
- Called before glfwInit or after glfwTerminate.
- GLFW_PLATFORM_ERROR
- The operating system or window system reported a failure; the description says which.
Example
1while (!glfwWindowShouldClose(window)) {
2 render();
3 glfwSwapBuffers(window);
4 glfwPollEvents(); // callbacks run here
5}