Input Mapping and Actions
Gameplay code should never ask "is the space bar down?". It should ask "does the player want to jump?". The answer may come from the space bar, the gamepad's south button, a touch button or a replay file, and the player may have rebound it. An input mapping layer sits between the platform layer's raw events and gameplay, and turns devices into actions.
Unity's Input System, Unreal's Enhanced Input and Godot's Input Map all follow this design:
| Concept | Meaning | Example |
|---|---|---|
| Action | something the player wants to do | Jump, Fire, Move, Pause |
| Binding | a device control that drives an action | Space → Jump; Gamepad South → Jump; left stick → Move |
| Button action | on/off, with edges | pressed (this frame), held, released (this frame) |
| Axis action | a value in -1..1 (or a 2D vector) | MoveX from A/D keys and the stick, combined |
| Context | a set of bindings that is active together | Gameplay, Menu, Vehicle; the topmost context can consume input |
Edges, held state and axes
Each frame the mapper starts from the raw device state (which the platform layer updated from events), evaluates every binding, and stores per action: current value, previous value and the derived edges. Keyboard keys and stick axes feed the same action, and the larger magnitude wins:
#include <cmath>
#include <cstdio>
#include <map>
#include <string>
#include <vector>
enum class Control { KeySpace, KeyA, KeyD, KeyEsc, PadSouth, PadStickX };
struct DeviceState { // filled by the platform layer from events
std::map<Control, float> value; // buttons are 0 or 1, sticks -1..1
};
struct Binding { Control control; float scale = 1; };
struct Action {
std::vector<Binding> bindings;
float value = 0, previous = 0;
bool held() const { return std::fabs(value) > 0.5f; }
bool pressed() const { return held() && !(std::fabs(previous) > 0.5f); }
bool released() const { return !held() && std::fabs(previous) > 0.5f; }
};
float applyDeadzone(float v, float dz) { // rescale so output starts at 0 just outside the deadzone
float a = std::fabs(v);
if (a < dz) return 0;
return std::copysign((a - dz) / (1 - dz), v);
}
struct InputMap {
std::map<std::string, Action> actions;
void update(const DeviceState& dev) {
for (auto& [name, a] : actions) {
a.previous = a.value;
float best = 0;
for (auto& b : a.bindings) {
auto it = dev.value.find(b.control);
float v = it == dev.value.end() ? 0 : it->second;
if (b.control == Control::PadStickX) v = applyDeadzone(v, 0.2f);
v *= b.scale;
if (std::fabs(v) > std::fabs(best)) best = v; // strongest input wins
}
a.value = best;
}
}
};
int main() {
InputMap input;
input.actions["Jump"].bindings = {{Control::KeySpace}, {Control::PadSouth}};
input.actions["MoveX"].bindings = {{Control::KeyA, -1}, {Control::KeyD, +1}, {Control::PadStickX}};
std::vector<DeviceState> frames = {
{{}},
{{{Control::KeySpace, 1}}},
{{{Control::KeySpace, 1}, {Control::KeyD, 1}}},
{{{Control::KeyD, 1}}},
{{{Control::PadStickX, -0.15f}}}, // stick drift inside the deadzone
{{{Control::PadStickX, -0.6f}, {Control::PadSouth, 1}}},
};
for (std::size_t f = 0; f < frames.size(); f++) {
input.update(frames[f]);
const Action& jump = input.actions["Jump"];
std::printf("frame %zu: Jump pressed=%d held=%d released=%d MoveX=%+.2f\n", f, jump.pressed(),
jump.held(), jump.released(), input.actions["MoveX"].value);
}
}frame 0: Jump pressed=0 held=0 released=0 MoveX=+0.00
frame 1: Jump pressed=1 held=1 released=0 MoveX=+0.00
frame 2: Jump pressed=0 held=1 released=0 MoveX=+1.00
frame 3: Jump pressed=0 held=0 released=1 MoveX=+1.00
frame 4: Jump pressed=0 held=0 released=0 MoveX=+0.00
frame 5: Jump pressed=1 held=1 released=0 MoveX=-0.50
The deadzone removes stick drift (frame 4 reads exactly 0) and rescales the rest, so a stick pushed just past the deadzone gives a small value instead of jumping to 0.2. For 2D sticks use a radial deadzone on the vector length, not one per axis, or diagonal movement feels sticky.
Contexts and consumption
When the pause menu is open, Escape should close the menu, not also pause again underneath, and the jump button should not make the character jump behind the menu. A context stack solves this: contexts are evaluated from the top, and a context that handles a control consumes it so lower contexts never see it.
Edges and the fixed timestep
The mapper runs once per rendered frame, but gameplay runs in fixed steps: zero, one or several per frame (see The Game Loop). If gameplay reads pressed() directly, a press on a frame with zero steps is lost, and a press on a frame with two steps jumps twice. The fix is to latch edges until a fixed step consumes them, and the same trick gives you a short input buffer (jump pressed a few steps before landing still counts):
#include <cstdio>
#include <vector>
struct LatchedButton {
int bufferedSteps = 0; // > 0 while a press is waiting to be consumed
void onFramePressed(int bufferWindow) { bufferedSteps = bufferWindow; }
bool consume() { // called by a fixed step that wants the action
if (bufferedSteps > 0) { bufferedSteps = 0; return true; }
return false;
}
void tick() { if (bufferedSteps > 0) bufferedSteps--; } // expire old presses
};
int main() {
struct Frame { int fixedSteps; bool jumpPressed; };
// frame 1 has a press but no fixed step; frame 3 runs two steps
std::vector<Frame> frames = {{1, false}, {0, true}, {1, false}, {2, true}, {1, false}};
LatchedButton jump;
bool onGround[] = {false, true, true, true, true, true, true}; // lands at step 1
int step = 0;
for (std::size_t f = 0; f < frames.size(); f++) {
if (frames[f].jumpPressed) jump.onFramePressed(6); // remember for up to 6 steps (0.1 s)
for (int s = 0; s < frames[f].fixedSteps; s++, step++) {
bool grounded = onGround[step];
if (grounded && jump.consume()) std::printf("frame %zu step %d: JUMP\n", f, step);
else std::printf("frame %zu step %d: %s\n", f, step, grounded ? "grounded" : "in air");
jump.tick();
}
}
}frame 0 step 0: in air
frame 2 step 1: JUMP
frame 3 step 2: JUMP
frame 3 step 3: grounded
frame 4 step 4: grounded
The press in frame 1 (which ran no step) is not lost; it fires on the next step. The press in frame 3 fires once, not twice, even though that frame ran two steps.
Rebinding and display
- Store bindings in the user config as text (
Jump = Key:Space, Pad:South), never as raw enum numbers that change between versions. - Show prompts from the binding, not from hard-coded text: "Press [Space]" on keyboard, the south-button glyph on a gamepad. Switch prompts when the last-used device changes.
- Bind movement keys by scancode (physical position) and show their keycode names, as explained in The Platform Layer.
- Let the player rebind everything, including menu keys. Accessibility guidelines for games list full remapping as a basic requirement.
Test yourself
Why does gameplay read actions such as Jump instead of KeySpace?
- Actions are faster to read
- One action can be driven by several devices and rebound without changing gameplay code
- Keys cannot be read directly
- It saves memory
A jump press happens on a frame that runs zero fixed steps. Without latching, what goes wrong?
- The player jumps twice
- The press is lost, because no step saw the 'pressed' edge
- The game crashes
- Nothing
What does a stick deadzone protect against?
- Input lag
- Small readings from a worn or resting stick (drift) moving the character
- Key repeat
- Double presses