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
MethodReturns
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>
ConstantKey
GLFW_KEY_W/A/S/DWASD
GLFW_KEY_SPACESpace bar
GLFW_KEY_ESCAPEEscape
GLFW_KEY_LEFT_SHIFTLeft Shift
GLFW_KEY_ENTEREnter/Return
GLFW_KEY_1 - GLFW_KEY_9Number keys
GLFW_KEY_UP/DOWN/LEFT/RIGHTArrow keys
GLFW_KEY_FF key
GLFW_KEY_Q/EQ, E
GLFW_KEY_TABTab

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))
MethodReturns
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

ConstantButton
GLFW_MOUSE_BUTTON_LEFTLeft click
GLFW_MOUSE_BUTTON_RIGHTRight click
GLFW_MOUSE_BUTTON_MIDDLEMiddle 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;
}