#Input & Controls
Header: Core/include/Core/Input.h
#Keyboard
Access keyboard state through engine.GetInput():
auto &input = engine.GetInput();
if (input.IsKeyDown(GLFW_KEY_W)) // true while held
if (input.IsKeyPressed(GLFW_KEY_SPACE)) // true for one frame on press
if (input.IsKeyReleased(GLFW_KEY_E)) // true for one frame on release| Method | Returns |
|---|---|
IsKeyDown(key) | true every frame the key is held |
IsKeyPressed(key) | true only the first frame it is pressed |
IsKeyReleased(key) | true only the frame it is released |
#Common Key Constants
#include <GLFW/glfw3.h>| Constant | Key |
|---|---|
GLFW_KEY_W/A/S/D | WASD |
GLFW_KEY_SPACE | Space bar |
GLFW_KEY_ESCAPE | Escape |
GLFW_KEY_LEFT_SHIFT | Left Shift |
GLFW_KEY_ENTER | Enter/Return |
GLFW_KEY_1 - GLFW_KEY_9 | Number keys |
GLFW_KEY_UP/DOWN/LEFT/RIGHT | Arrow keys |
GLFW_KEY_F | F key |
GLFW_KEY_Q/E | Q, E |
GLFW_KEY_TAB | Tab |
#Mouse
auto &input = engine.GetInput();
glm::vec2 pos = input.GetMousePosition(); // screen-space position
glm::vec2 delta = input.GetMouseDelta(); // movement since last frame
float scroll = input.GetScrollDelta(); // scroll wheel amount
if (input.IsMouseButtonDown(GLFW_MOUSE_BUTTON_LEFT))
if (input.IsMouseButtonPressed(GLFW_MOUSE_BUTTON_RIGHT))
if (input.IsMouseButtonReleased(GLFW_MOUSE_BUTTON_MIDDLE))| Method | Returns |
|---|---|
GetMousePosition() | Current mouse position as glm::vec2 |
GetMouseDelta() | Movement since last frame |
GetScrollDelta() | Scroll wheel delta (positive = up) |
IsMouseButtonDown(button) | true while held |
IsMouseButtonPressed(button) | true only first frame |
IsMouseButtonReleased(button) | true only on release |
#Mouse Button Constants
| Constant | Button |
|---|---|
GLFW_MOUSE_BUTTON_LEFT | Left click |
GLFW_MOUSE_BUTTON_RIGHT | Right click |
GLFW_MOUSE_BUTTON_MIDDLE | Middle click |
#Action Mapping
Map logical actions to multiple keys for cleaner code:
void OnStart(flecs::entity e, Engine &engine) override {
auto &input = engine.GetInput();
input.MapAction("jump", {GLFW_KEY_SPACE, GLFW_KEY_W});
input.MapAction("fire", {GLFW_MOUSE_BUTTON_LEFT});
}
void OnUpdate(flecs::entity e, Engine &engine, float dt) override {
auto &input = engine.GetInput();
if (input.IsActionPressed("jump")) {
// player jumped
}
if (input.IsActionDown("fire")) {
// firing weapon
}
}#Input Contexts
Swap input schemes for different game states:
engine.GetInput().PushContext("menu"); // disable gameplay input
engine.GetInput().PopContext(); // resume gameplay input#Example: WASD Movement with Mouse Look
void OnUpdate(flecs::entity e, Engine &engine, float dt) override {
auto &input = engine.GetInput();
auto *t = e.get_mut<Transform>();
if (!t)
return;
// Mouse look (only when right-click held)
if (input.IsMouseButtonDown(GLFW_MOUSE_BUTTON_RIGHT)) {
glm::vec2 delta = input.GetMouseDelta();
t->rotation.y += delta.x * 0.15f;
t->rotation.x -= delta.y * 0.15f;
t->rotation.x = glm::clamp(t->rotation.x, -89.0f, 89.0f);
}
// WASD movement relative to facing direction
float speed = 5.0f * dt;
float yaw = glm::radians(t->rotation.y);
glm::vec3 forward(std::sin(yaw), 0.0f, std::cos(yaw));
glm::vec3 right(std::cos(yaw), 0.0f, -std::sin(yaw));
if (input.IsKeyDown(GLFW_KEY_W)) t->position += forward * speed;
if (input.IsKeyDown(GLFW_KEY_S)) t->position -= forward * speed;
if (input.IsKeyDown(GLFW_KEY_A)) t->position -= right * speed;
if (input.IsKeyDown(GLFW_KEY_D)) t->position += right * speed;
}