Skip to content
Shmellyorc edited this page Aug 27, 2026 · 1 revision

Input

Void provides three input systems: keyboard, mouse, and gamepad. Each system provides snapshot-based state access with zero garbage collection. An action system allows named input bindings that work across all three devices.

Keyboard

The keyboard system provides access to all keyboard keys with bit-packed state storage for performance.

Get the current keyboard state:

var state = Keyboard.GetState();

Check individual keys:

if (state.IsKeyDown(KeyboardKey.W)) MoveForward();
if (state.IsKeyUp(KeyboardKey.Escape)) ExitGame();

Check lock states:

if (state.CapsLock) // Caps Lock is on
if (state.NumLock)  // Num Lock is on

Get all pressed keys:

var pressedKeys = state.GetPressedKeys();

Mouse

The mouse system provides access to position, buttons, and scroll wheel.

Get the current mouse state:

var state = Mouse.GetState();

Check buttons:

if (state.IsButtonPressed(MouseButton.Left)) SelectObject();
if (state.IsButtonReleased(MouseButton.Right)) CloseMenu();

Get position:

Vect2 position = state.Position;
float x = state.X;
float y = state.Y;

Get scroll wheel delta (positive = up/forward, negative = down/backward):

int scroll = state.ScrollWheel;

Set mouse position:

Mouse.SetPosition(100, 200);

Gamepad

The gamepad system supports up to four connected controllers with SDL database mapping. The database includes mappings for hundreds of controllers including Xbox, PlayStation, and Nintendo Switch controllers.

Update all gamepads once per frame:

Gamepad.UpdateAll();

Get the state of the first gamepad:

var state = Gamepad.GetState();

Check buttons:

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

Get thumbstick positions:

Vect2 leftStick = state.LeftStick;
Vect2 rightStick = state.RightStick;

Get trigger values:

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

Get force values for specific directions:

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

Gamepads have a configurable dead zone. The default is 0.15:

GameSettings.Instance.SetDeadZone(0.2f);

Get state for a specific player:

var playerState = Gamepad.GetState(PlayerIndex.One);
Gamepad.Update(PlayerIndex.Two);

Input Actions

The action system lets you define named actions with multiple bindings. An action can be bound to keyboard keys, mouse buttons, and gamepad buttons simultaneously.

Defining Actions with Strings

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);

Defining Actions with Enums

Using enums prevents typos and is recommended for larger projects.

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

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

InputAction.AddAction(Actions.Pause)
    .AddKey(KeyboardKey.Escape)
    .AddGamepad(GamepadButton.Start);

Checking Action States

Get the action state snapshot once per frame:

var state = InputAction.GetState();

Using string names:

if (state.IsPressed("Jump")) Jump();
if (state.IsHeld("MoveLeft")) MoveLeft();
if (state.IsReleased("Pause")) TogglePause();

Using enums:

if (state.IsPressed(Actions.Jump)) Jump();
if (state.IsHeld(Actions.MoveLeft)) MoveLeft();
if (state.IsReleased(Actions.Pause)) TogglePause();

Detailed State Information

Get the full trigger state of an action:

var actionState = state.GetState(Actions.Sprint);
switch (actionState)
{
    case ActionState.Pressed: StartSprint(); break;
    case ActionState.Held: ContinueSprint(); break;
    case ActionState.Released: StopSprint(); break;
    case ActionState.Up: break;
}

Action States Explained

  • Pressed: The action was just pressed this frame (transition from Up/Released to active)
  • Held: The action is currently held down (active for multiple consecutive frames)
  • Released: The action was just released this frame (transition from active to Up/Released)
  • Up: The action is not active

Input Focus

When IgnoreInputWhenUnfocused is enabled, all input is ignored when the game window is not focused. This is enabled by default:

GameSettings.Instance.SetIgnoreInputWhenUnfocused(true);

Back to Home

Clone this wiki locally