GLFW: input & timeGLFW 3.0
glfwSetInputMode
Changes how input behaves: hide or capture the cursor, sticky keys, raw mouse motion…
Signature
void glfwSetInputMode(GLFWwindow* window, int mode, int value);
How it works
The most used mode is the cursor. A first-person camera wants GLFW_CURSOR_DISABLED: the cursor disappears, stays locked to the window, and its position grows without limit, so the player can turn around forever without the mouse hitting the edge of the screen.
Press a key to go back to GLFW_CURSOR_NORMAL when the player opens a menu.
Parameters
windowGLFWwindow*
The window to change.
modeint
Which setting.
Common values
- GLFW_CURSOR
- Cursor behaviour; value is one of the four below.
- GLFW_CURSOR_NORMAL
- Visible and free (default).
- GLFW_CURSOR_HIDDEN
- Invisible over the window, but free to leave it.
- GLFW_CURSOR_DISABLED
- Invisible, locked, unlimited motion: for camera control.
- GLFW_CURSOR_CAPTURED
- GLFW 3.4: visible but cannot leave the window (strategy games).
- GLFW_STICKY_KEYS
- GLFW_TRUE: glfwGetKey reports GLFW_PRESS once even if the key was already released, so quick taps are not lost.
- GLFW_STICKY_MOUSE_BUTTONS
- The same for mouse buttons.
- GLFW_RAW_MOUSE_MOTION
- GLFW_TRUE: with the cursor disabled, use unaccelerated mouse motion. Check glfwRawMouseMotionSupported first.
- GLFW_LOCK_KEY_MODS
- GLFW_TRUE: callbacks also get GLFW_MOD_CAPS_LOCK and GLFW_MOD_NUM_LOCK in mods.
valueint
The new value for that mode.
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_INVALID_ENUM
- A hint, mode, key or button argument is not a valid GLFW constant.
- GLFW_PLATFORM_ERROR
- The operating system or window system reported a failure; the description says which.
- GLFW_FEATURE_UNAVAILABLE
- This platform cannot do it; Wayland, for example, does not let a program move its windows.
Example
example.cpp
1glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
2if (glfwRawMouseMotionSupported())
3 glfwSetInputMode(window, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE);
4
5// Tab frees the mouse for the menu
6if (glfwGetKey(window, GLFW_KEY_TAB) == GLFW_PRESS)
7 glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);