Input
rmp::input gives what the player
presses a name, and decides who gets to hear it. raylib answers "is the space bar down?"; a game
wants to know "did the player jump?", and whether that press was meant for the game at all or for
the button under the player's finger.
rmp::input::action("jump", KEY_SPACE, KEY_W, GAMEPAD_BUTTON_RIGHT_FACE_DOWN);
if (rmp::input::just_pressed("jump")) jump();It
is one header, #include <rmp/input.h>. The entry point reads the devices once at
the top of every frame, before the frame hook runs, so every question asked during that frame gets
the same answer. In a game started with RMP_GAME that is before any scene updates, and
two scenes in the same frame cannot disagree about what is held down. A game that runs its own frame
with RMP_ENTRY_POINT gets the same reading before its on_frame, and reads
its actions there exactly as a scene does.
Actions#
An action is a name and the bindings that press it, as many as you like.
rmp::input::action takes the name first and the
bindings after it:
rmp::input::action("jump", KEY_SPACE, KEY_W, GAMEPAD_BUTTON_RIGHT_FACE_DOWN);
rmp::input::action("fire", MOUSE_BUTTON_LEFT, KEY_J, GAMEPAD_BUTTON_RIGHT_TRIGGER_1);Each binding is a plain raylib constant, and its type says which device it belongs to.
KeyboardKey, MouseButton and GamepadButton are three different
enums in C++, so the framework knows at compile time that KEY_SPACE is a key and
MOUSE_BUTTON_LEFT a mouse button. Anything else is a compile error at the call, which is
also why there is no string of key names to spell wrong:
rmp::input::action("jump", 32); // an int is not a keyinclude/rmp/input.h:97:28: error: no matching function for call to 'to_binding'
97 | detail::define(name, { detail::to_binding(first), detail::to_binding(rest)... });
| ^~~~~~~~~~~~~~~~~~
snippet.cpp:1:13: note: in instantiation of function template specialization 'rmp::input::action<int>' requested here
1 | rmp::input::action("jump", 32); // an int is not a key
| ^An action with no binding at all does not compile either: there is no version of
action() that takes the name alone.
Defining one again replaces it#
Calling action() with a name that already exists replaces its bindings; it does not
add to them. That is what makes the natural place for these calls safe — a scene's
_ready(), which runs every time the player enters the scene:
void _ready() override {
rmp::input::action("jump", KEY_SPACE, GAMEPAD_BUTTON_RIGHT_FACE_DOWN);
rmp::input::action("fire", MOUSE_BUTTON_LEFT, GAMEPAD_BUTTON_RIGHT_TRIGGER_1);
}It is also how a game rebinds a key at runtime: call action() again with the new
list. An action holds up to eight bindings; more than that keeps the first eight and says so in the
log: INPUT: "…" was given … bindings and the limit is …; the extra ones are ignored.
Reading an action#
Three questions, each asked by name:
| Call | True |
|---|---|
pressed("jump") | Every frame any of its bindings is held down. |
just_pressed("jump") | The frame one of them goes down. |
just_released("jump") | The frame one of them comes up. |
The edges compare this frame with the one before, and the first frame of the game is compared with itself: a key already held when the game starts does not read as just pressed and fire whatever it is bound to.
An action nobody has defined reads as false, and the log says so once per name — not sixty times a second: INPUT: no action called followed by the name. A misspelt name is the usual reason.
Half of a stick#
An analogue stick reads from -1 to +1 and an action is on or off, so a stick binding names one
half of an axis. rmp::input::stick is the one place
you write something that is not a plain raylib constant:
rmp::input::action("aim_left", KEY_Q, rmp::input::stick(GAMEPAD_AXIS_RIGHT_X, -1));
rmp::input::action("aim_right", KEY_E, rmp::input::stick(GAMEPAD_AXIS_RIGHT_X, 1));The sign says which half counts: -1 is left or up, +1 right or down. Any negative number is taken as -1 and any other, 0 included, as +1, because a stick has two halves and there is no third thing to mean. The half counts as pressed once the stick is past the dead zone, described below.
Which gamepad#
Every gamepad binding reads the first gamepad raylib knows, index 0. A game for several players on
one machine reads the others from raylib itself, with IsGamepadButtonDown() and the
gamepad's index.
The actions that come with it#
Six actions exist before the game defines any, so that movement works with no setup at all. They are ordinary actions: defining one of these names again replaces it like any other.
| Action | Keyboard | Gamepad |
|---|---|---|
move_left | KEY_A, KEY_LEFT | d-pad left, left stick to the left |
move_right | KEY_D, KEY_RIGHT | d-pad right, left stick to the right |
move_up | KEY_W, KEY_UP | d-pad up, left stick up |
move_down | KEY_S, KEY_DOWN | d-pad down, left stick down |
ui_accept | KEY_ENTER, KEY_SPACE | the bottom face button |
ui_cancel | KEY_ESCAPE | the right face button |
ui_accept and ui_cancel are there for the game to read. The interface
does not read them: rmp::ui watches its own keys for moving
the focus, pressing a button, and backing out of a text field or an open list (see
focus, keyboard and gamepad), so
redefining ui_accept does not change what presses a focused button.
The four movement actions each take a letter and an arrow. Two players at one keyboard, the second on the arrows, means taking the arrows away from the first: define the first player's actions again without them, or the up arrow moves both. Pong does exactly that (main.cpp):
void _ready() override {
rmp::input::action("p2_up", KEY_UP);
rmp::input::action("p2_down", KEY_DOWN);
// Player one keeps W, S, the d-pad and the stick.
rmp::input::action("move_up", KEY_W, GAMEPAD_BUTTON_LEFT_FACE_UP,
rmp::input::stick(GAMEPAD_AXIS_LEFT_Y, -1));
rmp::input::action("move_down", KEY_S, GAMEPAD_BUTTON_LEFT_FACE_DOWN,
rmp::input::stick(GAMEPAD_AXIS_LEFT_Y, 1));
}Moving: vector and axis#
rmp::input::vector() is a direction from four
actions, and with no arguments it uses the four move_ actions above — the keyboard, the
d-pad and the left stick at once:
velocity = rmp::input::vector() * 200;Three things are decided for you in that line:
- It is normalised. Holding right and down gives a vector of length 1, not 1.41, so going diagonally is not 41 % faster than going straight. Only a vector longer than 1 is shortened: a stick pushed halfway still gives half.
- +Y is down, as in raylib's screen coordinates and an object's
position, so "up" gives a negative Y and nothing has to be flipped. - Opposite directions cancel. Left and right held together give 0, which is what a player mashing both keys expects.
rmp::input::axis() is the same thing on one axis:
-1, 0 or +1 from move_left and move_right, or the analogue value in between
when a stick is what is moving it. A platformer walks with it:
velocity.x = rmp::input::axis() * 180;Both take your own actions too, in the order negative then positive:
float aim = rmp::input::axis("aim_left", "aim_right");
rmp::input::action("port", KEY_A);
rmp::input::action("starboard", KEY_D);
rmp::input::action("ahead", KEY_W);
rmp::input::action("astern", KEY_S);
Vector2 boat = rmp::input::vector("port", "starboard", "ahead", "astern");Each action contributes how far it is pushed, from 0 to 1. A key or a button is all or nothing; a stick is analogue. When an action has several bindings the strongest one wins, so a key held down gives a full 1 even if the stick on the same action is only half pushed.
For a stick's raw reading — a camera that turns with the right stick, say —
rmp::input::axis_value() gives one axis from
-1 to +1, dead zone applied, and is never routed:
float turn = rmp::input::axis_value(GAMEPAD_AXIS_RIGHT_X);The dead zone#
A stick at rest does not read exactly 0, and a worn one drifts further. The dead zone is the part
of the travel that reads as zero, as a fraction of it, and it starts at [input] deadzone
in raylib_multiplatform.toml:
[input]
deadzone = 0.25Past the zone the reading is rescaled so that it starts again from 0: just outside a dead zone of 0.2 the stick reads a little above zero, not a sudden 0.2, and a character does not lurch into motion at a fifth of its speed. Keys and the d-pad have no travel and are unaffected.
The value has to be between 0 and 0.95. Anything else is refused when the configuration is read, before a build starts, with the reason:
[input]
deadzone = 1.0 raylib_multiplatform.toml:2: deadzone = 1.0
^^^^^^^^^^^^^^
configure: error: [input] deadzone = 1.0 has to be between 0 and 0.95.
It is the fraction of a stick's travel that reads as zero. 0 turns it off, which on a worn controller means the character walks on its own; past 0.95 there is almost no travel left to read.At runtime, rmp::input::deadzone() reads it and
rmp::input::set_deadzone() changes it — what a
settings slider calls. A value outside 0..0.95 is clamped rather than refused, so a slider cannot
switch the sticks off:
static float deadzone = rmp::input::deadzone();
if (rmp::ui::slider("Stick dead zone", deadzone, 0.0f, 0.5f)) rmp::input::set_deadzone(deadzone);The pointer#
The mouse and a finger are one pointer. raylib already reports the first touch as the mouse, so
the same code works on a desktop and on a phone, with no #if:
| Call | Gives |
|---|---|
pointer_screen() | Where it is, in pixels, from the top-left of the window. |
pointer() | Where it is in the world, through the camera of the scene on top of the stack: what you compare an object's position with. With no scene on the stack, as in a game on RMP_ENTRY_POINT without scenes, there is no camera, and it is pointer_screen(). The same as pointer_screen() while that camera has not moved and the window is the size [window] width and height ask for. The camera puts its position, which starts at the middle of that size, in the middle of the game's area, scaled as [window] stretch says, so in a window of any other size — resized, or a phone's screen — the two differ even when the camera has not moved. On a letterboxed window, a pointer on a bar is outside the world. |
pointer_delta() | How far it moved since the last frame, in pixels. Zero on the first frame. |
pointer_down() | The left button, or a finger, is held. |
pointer_pressed() | The frame it goes down. |
pointer_released() | The frame it comes up. |
The last three are the left mouse button, read raw: they are not routed, so a press on an
interface button reads as pointer_pressed() too. To act only on presses the interface
did not take, ask consumed_pointer() first —
or bind the button to an action, which asks for you:
if (rmp::input::pointer_pressed() && !rmp::input::consumed_pointer()) {
spawn({ .position = rmp::input::pointer() });
}An object that wants to be clicked does not need any of this: its
on_click already gives the interface first
refusal.
Who hears a press#
A finger that presses a button on the HUD must not also fire the gun under it, and typing a name
into a text field must not walk the player across the level. Both have to be true without anyone
writing an if, so a query by name is routed, and a query by
device is not:
if (rmp::input::just_pressed("fire")) shoot(); // routed: quiet while something above has the input
if (rmp::input::pressed(KEY_W)) walk(); // raw: exactly IsKeyDown(KEY_W), alwaysThe argument's type says which of the two you asked for, and you can see it at the call. The raw
versions — pressed, just_pressed and just_released given a
key, a mouse button or a gamepad button constant instead of a name — are there for
when you want to know regardless of who else is listening: a held Shift that makes the player run,
a debug key.
The three rules#
An action reads as false while something above the code asking for it has taken its device. It is decided binding by binding, so an action on both a key and a mouse button still answers to the key while the mouse is over a button.
- Mouse bindings go quiet while the interface wants the pointer:
while
rmp::ui::wants_pointer()is true. That is while the pointer is over anything the interface paints — a control, a panel or any box with a background or a border, an image, a scroll area, empty part included — while a slider is being dragged, and while a dropdown's list is open. Plain text with nothing behind it does not take the pointer, so a score drawn over the game can be clicked through. - Key and gamepad bindings go quiet while the interface wants the keyboard: while
rmp::ui::wants_keyboard()is true, which is while a text field is typing — not merely while it has the focus (see typing is having the keyboard). Gamepad buttons and sticks go with the keyboard. - Every action goes quiet in a scene that another one covers, unless every scene
above it set
input_below, which isfalseby default. A covered scene is usually not updated at all; when the one on top setsupdates_below— a world that keeps moving under a menu — its_update()runs and every action it reads is false, and neither scene says so. See what a scene lets through.
_update()
runs before anything draws, so the interface answers it from what the last frame drew:
wants_pointer() for where the pointer is now, and wants_keyboard() for the
field that was typing, which still is. A finger that lands on
a button without hovering over it first is the button's in the frame it lands, so the tap does not
reach the game as well. A slider being dragged keeps wants_pointer() true wherever the
pointer goes, until it comes up.
In
a game of scenes, a frame in which no scene draws any interface ends with both false, so a pause
menu that pops while the pointer is over one of its buttons does not leave the game's mouse
dead.
For code that wants the reason and not the answer — a different cursor over the HUD, a raw read
that should still respect the interface —
consumed_pointer() and
consumed_keyboard() answer whether
anything above the caller is holding that device: a scene on top, or the interface.
What routing does not cover#
The interface's own navigation is not an action. Up, Down and Tab — and the d-pad and the left stick, up and down — move the focus between controls, and Left and Right move a focused slider. Enter, Space and the bottom face button press the focused one, and Escape and the right face button back out of a text field or an open list.
A
scene that reads one of those keys through an action — move_up, on the up arrow — is
taken by the interface for a game being played, and its controls for a HUD over it. They take no
focus by themselves, and the arrows stay the game's until Tab walks into the HUD; from then the keys
are the interface's, wants_keyboard() is true and the game's keyboard and gamepad
actions read nothing, until Escape, the right face button or a click elsewhere gives them back. So the
up arrow moves the player or the focus, never both. A menu is still best as a scene of its own,
pushed on top, with the game underneath frozen or, by the third rule, deaf;
a button in the HUD has the rest, and what
to say when the guess is wrong.
A whole example#
examples/input/01_actions defines its own actions, moves with
vector(), shoots with an action on the left mouse button and pushes a pause scene with a
button in the middle of the screen. Pressing that button does not shoot, and nothing in the code
says so:
// ---------------------------------------------------------------------------
// examples/input/01_actions/src/main.cpp
//
// Named actions, eight-direction movement in one line, and the part nobody
// thinks about until it bites: who gets to hear a press.
//
// Run it: `rmp example 01_actions`.
// ---------------------------------------------------------------------------
#include <rmp/app.h>
#include <rmp/input.h>
#include <rmp/scene.h>
#include <rmp/ui.h>
class PauseScene : public rmp::Scene {
public:
void _draw() override {
rmp::ui::begin();
rmp::ui::text("Paused");
if (rmp::ui::button("Resume")) rmp::Scene::pop();
rmp::ui::end();
}
};
class GameScene : public rmp::Scene {
public:
void _ready() override {
// The name first, then as many bindings as you like. The TYPE of each
// argument says which device it is — KeyboardKey, MouseButton and
// GamepadButton are three different enums — so passing anything else is
// a compile error, and there is no string to spell wrong.
rmp::input::action("jump", KEY_SPACE, GAMEPAD_BUTTON_RIGHT_FACE_DOWN);
rmp::input::action("fire", MOUSE_BUTTON_LEFT, GAMEPAD_BUTTON_RIGHT_TRIGGER_1);
rmp::input::action("pause", KEY_ESCAPE, GAMEPAD_BUTTON_MIDDLE_RIGHT);
// An analogue stick needs to know which half counts, and stick() is the
// only place you write something that is not a plain raylib constant.
rmp::input::action("aim_left", KEY_Q,
rmp::input::stick(GAMEPAD_AXIS_RIGHT_X, -1));
// Defining these here is safe even though the player enters this scene
// over and over: registering a name again REPLACES it, never adds to it.
}
void _update(float delta) override {
// Eight directions, keyboard and d-pad and stick at once, normalised so
// the diagonal is not 41 % faster — with no configuration at all. The
// six factory actions (move_*, ui_accept, ui_cancel) are already there,
// and they are ordinary actions you can redefine.
_position.x += rmp::input::vector().x * _speed * delta;
_position.y += rmp::input::vector().y * _speed * delta;
if (rmp::input::just_pressed("jump")) jump();
// AND THIS IS THE PART THAT MATTERS. "fire" is bound to the left mouse
// button, and there is a pause menu one push away with a Resume button
// in the middle of the screen. A finger that presses Resume must not
// also shoot.
//
// It does not, and nothing here says so. A query BY NAME is routed: an
// action on a mouse button goes quiet while the UI wants the pointer,
// one on a key goes quiet while a text field has the keyboard, and all
// of them go quiet in a scene with something above it that kept the
// input. Compare with what it takes by hand:
//
// if (!rmp::ui::wants_pointer() && IsMouseButtonPressed(0)) shoot();
//
if (rmp::input::just_pressed("fire")) shoot();
if (rmp::input::just_pressed("pause")) rmp::Scene::push<PauseScene>();
// The escape hatch, for when you want to know regardless of who else is
// listening. The argument type says which of the two you asked for, and
// you can see it at the call site.
if (rmp::input::pressed(KEY_LEFT_SHIFT))
_speed = 400.0f;
else
_speed = 200.0f;
}
void _draw() override {
DrawCircleV(_position, 16, SKYBLUE);
rmp::ui::begin({ .placement = rmp::ui::Align::TOP_LEFT });
// The pointer is one pointer: mouse on a desktop, finger on a phone,
// and the same code on every target with no #ifdef.
rmp::ui::text(rmp::input::pointer_down() ? "pointer down" : "pointer up");
rmp::ui::text(TextFormat("%d jumps, %d shots", _jumps, _shots));
rmp::ui::end();
}
private:
void jump() { ++_jumps; }
void shoot() { ++_shots; }
Vector2 _position{ 400, 225 };
float _speed = 200.0f;
int _jumps = 0;
int _shots = 0;
};
RMP_GAME(GameScene);
rmp example 01_actionsDropping to raylib#
Nothing here hides raylib. IsKeyPressed(), GetMousePosition(),
GetTouchPointCount(), GetGestureDetected() and the rest work as they always
do, next to rmp::input, and they are what to reach for when the game needs something
this module does not model: several touches at once, a second gamepad, text typed as characters with
GetCharPressed(). They are raw — the routing above does not apply to them — so a raw
read that should leave the interface alone asks
consumed_pointer() or
consumed_keyboard() first.