Skip to content
shmellyorc edited this page Sep 23, 2026 · 7 revisions

Input

VOID provides keyboard, mouse, gamepad, and action-based input.

SDL3 is used internally for platform input, but normal game code works with VOID's own input types.

Keyboard

Get a snapshot:

var state = Keyboard.GetState();

Check keys:

if (state.IsKeyDown(KeyboardKey.W))
    MoveForward();

if (state.IsKeyUp(KeyboardKey.Escape))
    KeepRunning();

Get currently pressed keys:

KeyboardKey[] pressed = state.GetPressedKeys();
int count = state.GetPressedKeyCount();

Keyboard snapshots also expose the current lock states:

bool capsLock = state.CapsLock;
bool numLock = state.NumLock;

Unknown or out-of-range KeyboardKey values are treated as up.

Mouse

var mouse = Mouse.GetState();

Buttons:

if (mouse.IsButtonPressed(MouseButton.Left))
    SelectObject();

if (mouse.IsButtonReleased(MouseButton.Right))
    CloseMenu();

The supported button names are:

MouseButton.Left
MouseButton.Right
MouseButton.Middle
MouseButton.Extra1
MouseButton.Extra2

Position and wheel:

Vect2 position = mouse.Position;
int wheel = mouse.ScrollWheel;

MouseState.Position is window-relative. Camera conversion methods work in the configured logical viewport, so account for window scaling before passing raw mouse coordinates to a camera when your window and viewport are not 1:1.

See Camera → Screen and World Conversion and Window & Displays → Window Scale Modes.

Move the cursor:

// Global desktop coordinates
Mouse.SetPosition(100, 200);

// Coordinates relative to a specific game window
Mouse.SetPosition(100, 200, Game.Instance);

Repeated mouse reads during the same game-loop frame return the same snapshot unless the cursor is repositioned.

Gamepads

Gamepads use VOID's public API with SDL3 gamepad support underneath.

Get the default state:

var state = Gamepad.GetState();

Buttons:

if (state.IsButtonPressed(GamepadButton.A))
    Jump();

Sticks and triggers:

Vect2 left = state.LeftStick;
Vect2 right = state.RightStick;

float leftTrigger = state.LeftTrigger;
float rightTrigger = state.RightTrigger;

Directional force:

float horizontal =
    state.GetForce(GamepadButton.LeftStickRight) -
    state.GetForce(GamepadButton.LeftStickLeft);

Dead zone:

GameSettings.Instance.SetDeadZone(0.20f);

Specific player:

var playerOne = Gamepad.GetState(PlayerIndex.One);

Input Actions

Actions let gameplay code refer to intent instead of a specific device.

Action names are case-sensitive, and one action can contain keyboard, mouse, and gamepad bindings.

Internally, action names are converted to cached 64-bit identifiers with HashHelper.Cache64. The action registry and retained action-state snapshots use those numeric identifiers instead of string dictionary keys. The public API still accepts strings and enums, with enums recommended for most games.

See HashHelper for the cached identifier system.

Define Actions

InputAction.AddAction("Jump")
    .AddKey(KeyboardKey.Space)
    .AddKey(KeyboardKey.Up)
    .AddGamepad(GamepadButton.A);

InputAction.AddAction("MoveLeft")
    .AddKey(KeyboardKey.Left)
    .AddKey(KeyboardKey.A)
    .AddGamepad(GamepadButton.DPadLeft)
    .AddGamepad(GamepadButton.LeftStickLeft);

Enums are useful for larger games:

public enum Actions
{
    Jump,
    MoveLeft,
    MoveRight,
    Pause
}

InputAction.AddAction(Actions.Jump)
    .AddKey(KeyboardKey.Space)
    .AddGamepad(GamepadButton.A);

Calling AddAction again with the same name returns the existing action.

Read Actions

The engine updates the action system once per game update. GetState() returns that snapshot; querying it does not poll devices again or advance transitions.

InputActionState remains an immutable snapshot, but VOID reuses the existing snapshot when the logical action state has not changed. Snapshot dictionaries use cached ulong action identifiers rather than string keys, reducing repeated string-key lookup and comparison work while preserving JustPressed, Pressed, JustReleased, and Up transition behavior.

The allocation investigation and before/after measurements are documented in Performance Benchmarks.

var actions = InputAction.GetState();

if (actions.IsJustPressed(Actions.Jump))
    StartJump();

if (actions.IsPressed(Actions.MoveLeft))
    MoveLeft();

if (actions.IsJustReleased(Actions.Pause))
    TogglePause();

if (actions.IsReleased(Actions.Jump))
{
    // Jump is currently up.
}

The helpers mean:

Query Meaning
IsJustPressed Changed from released to pressed on this update
IsPressed Is currently pressed, including the first pressed update
IsJustReleased Changed from pressed to released on this update
IsReleased Is currently released, including the first released update

Detailed transition state:

switch (actions.GetState(Actions.Jump))
{
    case ActionState.JustPressed:
        break;

    case ActionState.Pressed:
        break;

    case ActionState.JustReleased:
        break;

    case ActionState.Up:
        break;
}

A missing action, empty name, or default InputActionState is treated as released. Transition queries return false, IsPressed returns false, IsReleased returns true, and GetState returns ActionState.Up.

Manage Registered Actions

bool exists = InputAction.HasAction("Jump");

if (InputAction.TryGetAction("Jump", out var jump))
{
    // Inspect or add bindings.
}

InputAction.RemoveAction("Jump");
InputAction.Clear();

Ignore Input While Unfocused

VOID can suppress input while its window does not have focus:

GameSettings.Instance
    .SetIgnoreInputWhenUnfocused(true);

When enabled, keyboard keys and mouse buttons are reported as released while the game window is closed or unfocused. Mouse wheel movement is also suppressed.


Back to Home

Clone this wiki locally