-
Notifications
You must be signed in to change notification settings - Fork 1
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.
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();
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);
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);
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.
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);
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);
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();
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;
}
- 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
When IgnoreInputWhenUnfocused is enabled, all input is ignored when the game window is not focused. This is enabled by default:
GameSettings.Instance.SetIgnoreInputWhenUnfocused(true);