Game

Input Management

Keyboard, mouse, focus, and cursor APIs for gameplay systems.

Access

#include <EngineExports.hpp>
#include <input/InputManager.hpp>
#include <input/KeycodeMap.hpp>

auto& input = Public::Engine::getInstance().GetInputManager();

The engine updates input state during Engine::Loop(). Gameplay systems can read state during their update(float dt) calls.

Keyboard

Use Public::Input::Key::Code or the wrapper constants from KeycodeMap.hpp:

using Public::Input::Key::Code;

if (input.IsKeyDown(Code::W)) {
    // Held this frame
}

if (input.GetKeyDown(Code::Space)) {
    // Pressed this frame only
}

if (input.IsKeyUp(Code::Escape)) {
    // Not currently held
}

Common key groups:

GroupValues
LettersA through Z
NumbersNum0 through Num9
Function keysF1 through F12
NavigationLeft, Right, Up, Down, Home, End, PageUp, PageDown
ControlEscape, Space, Enter, Tab, Backspace, Shift, Ctrl, Alt, CapsLock

Raw integer key codes are also supported:

if (input.IsKeyDown(0x57)) {
    // W
}

Mouse

Public::Input::MouseState mouse = input.GetMouseState();

int x = mouse.x;
int y = mouse.y;
int dx = mouse.deltaX;
int dy = mouse.deltaY;
bool left = mouse.buttons[0];

Mouse API:

APIUse
UpdateMouseDelta()Refresh delta state. Usually called by the engine.
GetMouseState()Read position, delta, and up to 8 buttons.
IsMouseButtonDown(button)Check a button.
IsMouseButtonUp(button)Check a released button.
SetMousePosition(x, y)Move the cursor.

Cursor Lock

Free-look controls usually lock and hide the cursor while the window is focused:

if (input.GetKeyDown(Public::Input::Key::Code::F1)) {
    if (input.IsCursorLocked()) {
        input.UnlockCursor();
        input.ShowCursor();
    } else {
        input.LockCursor();
    }
}

Before consuming mouse motion, check focus:

if (!input.IsWindowFocused()) {
    return;
}

Nevo-base/game/systems/freecam/FreecamSystem.hpp uses this pattern for the reference free camera.

Freecam Movement Pattern

The sample Freecam system reads WASD/QE for local movement and mouse delta for yaw/pitch:

Eigen::Vector3f moveDir = Eigen::Vector3f::Zero();
if (input.IsKeyDown(Public::Input::Key::Code::W)) moveDir.z() += 1.0f;
if (input.IsKeyDown(Public::Input::Key::Code::S)) moveDir.z() -= 1.0f;
if (input.IsKeyDown(Public::Input::Key::Code::A)) moveDir.x() -= 1.0f;
if (input.IsKeyDown(Public::Input::Key::Code::D)) moveDir.x() += 1.0f;
if (input.IsKeyDown(Public::Input::Key::Code::Q)) moveDir.y() -= 1.0f;
if (input.IsKeyDown(Public::Input::Key::Code::E)) moveDir.y() += 1.0f;

Normalize before applying speed so diagonal movement is not faster.