Color legend plain property method
BorrowedTorch.InputBinder connects the physical inputs of a game, keys, buttons, sticks and triggers, to what they do: named actions such as “Jump”, “Fire” or “Look”. The game registers its actions once, with the bindings they start with. The player can rebind them later, in a settings menu, and the binder refuses to give one input to two actions without asking. Every frame the game hands the binder the inputs it polled, and the binder runs the callback of the action each one is bound to.
The binder reads no hardware itself. It takes whatever the game polled, from MonoInput, MonoGame or anything else, so it depends on neither and runs anywhere .NET does.
These pages cover the library from setup to every public type:
| Page | What it covers |
|---|---|
| Quick start guide (this page) | Installing, the parts and their namespaces, a first game’s actions, feeding the binder every frame, a first rebind |
| Actions | The three shapes, registering, callbacks, actions built ahead, names and descriptions, finding and unregistering actions |
| Inputs | What makes an input, sources and ids, the factories, buttons and axes, equality, showing an input to the player |
| Binding and rebinding | Slots, default bindings, binding and its results, conflicts, unbinding, a rebinding screen, saving bindings |
| Updating every frame | What an update does, which inputs to pass, reusing inputs, players, feeding the binder from MonoInput, threads |
| Modifier keys | Ctrl, Shift and Alt bindings, the most specific binding winning, a press keeping its binding, modifier keys as bindings |
| Glossary | Every public type with its members in alphabetical order, one page per type |
Installing
Add the package to your project:
dotnet add package BorrowedTorch.InputBinder
| Requirement | Version |
|---|---|
| .NET | 8 or 10 |
| Operating system | Any: the binder reads no hardware |
| MonoGame | Not needed. The two-axes values are System.Numerics.Vector2, which MonoGame 3.8.2 and later converts to its own Vector2 |
| Borrowed Torch packages | None. MonoInput is a good source of polled input, see feeding the binder from MonoInput |
The parts
- Actions. What the game can do, in three shapes: a
ButtonActionis told a button’s state, aOneAxisActionafloatsuch as a trigger’s, and aTwoAxesActionaVector2such as a stick’s. See actions. - Inputs. The physical controls: a
ButtonInput, aOneAxisInputor aTwoAxesInput, each on a keyboard, a mouse, a gamepad or a MIDI device.InputFactorymakes them in a few characters. An input is bound to an action of its own shape only. See inputs. - The binder.
InputBinderkeeps the actions and which inputs drive them. Registering an action returns aRegisteredActionHandle, which binds and unbinds the action’s inputs and reads them back for a settings menu. See binding and rebinding.
| Namespace | Types |
|---|---|
BorrowedTorch.InputBinding |
InputBinder |
BorrowedTorch.InputBinding.Factories |
InputFactory |
BorrowedTorch.InputBinding.Types |
ButtonState, ButtonModifier, InputSource, InputType, InputBinderResult: the enums |
BorrowedTorch.InputBinding.Types.Actions |
ButtonAction, OneAxisAction, TwoAxesAction |
BorrowedTorch.InputBinding.Types.Actions.Base |
BaseAction |
BorrowedTorch.InputBinding.Types.Inputs |
ButtonInput, OneAxisInput, TwoAxesInput |
BorrowedTorch.InputBinding.Types.Inputs.Base |
BaseInput |
BorrowedTorch.InputBinding.Types.Handles |
RegisteredActionHandle |
BorrowedTorch.InputBinding.Types.Results |
BindResult |
A first game
A player that jumps with Space or W, fires with the left mouse button or Ctrl, and looks around with the mouse. The actions are registered once, each with the bindings it starts with:
using BorrowedTorch.InputBinding;
using BorrowedTorch.InputBinding.Factories;
using BorrowedTorch.InputBinding.Types;
using BorrowedTorch.MonoInput.Inputs.Mouse;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
public class MyGame : Game
{
private readonly InputBinder _binder = new(); // two bindings per action
protected override void Initialize()
{
// a button callback gets every state polled, so it picks the ones it acts on
_binder.RegisterButtonAction("Jump",
(state, player) => { if (state == ButtonState.Pressed) _player.Jump(); },
description: "Jump over a gap")
.WithDefaultBinding(0, InputFactory.Key((int)Keys.Space))
.WithDefaultBinding(1, InputFactory.Key((int)Keys.W));
_binder.RegisterButtonAction("Fire",
(state, player) => { if (state == ButtonState.Pressed) _player.Fire(); },
description: "Fire the torch")
.WithDefaultBinding(0, InputFactory.MouseButton((int)MouseButtonTypes.Left))
.WithDefaultBinding(1, InputFactory.Key((int)Keys.LeftControl));
_binder.RegisterTwoAxesAction("Look",
(delta, player) => _camera.Turn(delta.X, delta.Y),
description: "Look around")
.WithDefaultBinding(0, InputFactory.MouseAxes(0)); // 0: the pointer's movement, an id of our own
base.Initialize();
}
}
_player and _camera stand for your own code. The numbers are ids the game chooses: here MonoGame’s Keys values
for keys and MonoInput’s MouseButtonTypes for mouse buttons. The binder only compares them, see
sources and ids. Two actions given the same default input throw at
startup, rather than one of them quietly going without it.
InputBinder members used here
| Member | Description |
|---|---|
InputBinder(int inputBindingsPerAction = 2) |
A binder whose actions have that many binding slots each. |
RegisterButtonAction(name, callback, description, representation) |
Registers a button action and returns its handle. Throws when an action of that name is already registered. |
RegisterTwoAxesAction(name, callback, description, representation) |
Registers a two-axes action and returns its handle. |
WithDefaultBinding(int index, BaseInput input) |
On the handle. Binds the input into a slot and returns the handle, for chaining. Throws when the input can’t be bound there. |
Feeding it every frame
Each frame the game polls its devices into inputs and passes them to Update, with the modifier keys held.
Here the input comes from MonoInput, whose managers update first:
using BorrowedTorch.InputBinding.Types.Inputs;
using BorrowedTorch.MonoInput.Inputs;
using BorrowedTorch.MonoInput.Inputs.Keyboard;
// polled inputs, made once and refilled every frame: Update then allocates nothing
private readonly ButtonInput[] _keys = new ButtonInput[256]; // one per Keys value, made at its first press
private readonly ButtonInput _leftButton = InputFactory.MouseButton((int)MouseButtonTypes.Left);
private readonly TwoAxesInput[] _pointer = [InputFactory.MouseAxes(0)];
private readonly List<ButtonInput> _buttons = new(32);
protected override void Update(GameTime gameTime)
{
KeyboardManager.Update();
MouseManager.Update();
_buttons.Clear();
// every key pressed, held or just let go
foreach (var status in KeyboardManager.ActiveKeys)
{
var key = _keys[(int)status.Key] ??= InputFactory.Key((int)status.Key);
key.State = ToBinder(status.State);
_buttons.Add(key);
}
var mouse = MouseManager.MouseStatus;
_leftButton.State = ToBinder(mouse.LeftButton.State);
_buttons.Add(_leftButton);
_pointer[0].Value = new System.Numerics.Vector2(mouse.PositionDelta.X, mouse.PositionDelta.Y);
// runs Jump, Fire and Look for the inputs bound to them
_binder.Update(_buttons, null, _pointer,
withShift: KeyboardManager.IsShiftDown,
withCtrl: KeyboardManager.IsCtrlDown,
withAlt: KeyboardManager.IsAltDown);
base.Update(gameTime);
}
// MonoInput's states to the binder's: Idle is None
private static ButtonState ToBinder(ButtonStates state) => state switch
{
ButtonStates.Pressed => ButtonState.Pressed,
ButtonStates.Down => ButtonState.Down,
ButtonStates.Up => ButtonState.Up,
_ => ButtonState.None
};
The binder looks each input up by what it is, its source, shape, id and modifier, and calls the bound action with its
state or value and its player number. An input bound to nothing is skipped. Every input passed calls its action,
whatever its state, which is why the callbacks above check for Pressed: the mouse button is passed every frame, idle
or not. The updating page has the details, and an adapter for the whole of MonoInput:
gamepads included.
InputBinder update method
| Method | Description |
|---|---|
Update(buttonInputs, oneAxisInputs, twoAxesInputs, withShift, withCtrl, withAlt) |
Runs the action bound to each input of the three lists, any of which may be null. The flags say which modifier keys are held. |
A first rebind
In a settings menu, the player picks Jump and presses J. The handle binds it, and reports a conflict rather than take J from another action:
var jump = _binder.GetAction("Jump");
var result = jump.Bind(0, InputFactory.Key((int)Keys.J));
if (result.Result == InputBinderResult.InputBoundToOtherAction)
{
// J belongs to result.ConflictingActionHandle: ask the player, then take it
jump.Bind(0, InputFactory.Key((int)Keys.J), forceMap: true);
}
Space, which was in that slot, is released, and from the next update J jumps. The binding page covers the slots, every result, and saving the player’s bindings.
Members used here
| Member | Description |
|---|---|
InputBinder.GetAction(string actionName) |
The handle of the action of that name, or null. |
RegisteredActionHandle.Bind(int index, BaseInput input, bool forceMap = false) |
Binds the input into the slot, and returns a BindResult. With forceMap, takes the input from the action that has it. |
BindResult.Result |
InputBinderResult. Success, or why the input wasn’t bound. |
BindResult.ConflictingActionHandle |
The handle of the action that has the input, on a conflict. |