menu_bookDocumentation
Input - s&box Gameplay Documentation
Input
s&box provides a comprehensive input system that handles keyboard, mouse, and gamepad input. The input system uses named actions defined in your project settings, making it easy to support multiple input devices.
Checking Input
Input is checked in your component's Update methods:
CSHARP
protected override void OnUpdate()
{
// Check if a button was just pressed this frame
if ( Input.Pressed( "Jump" ) )
{
Jump();
}
// Check if a button is being held down
if ( Input.Down( "Sprint" ) )
{
speed *= 2;
}
// Check if a button was released this frame
if ( Input.Released( "Fire" ) )
{
StopFiring();
}
}Analog Input
Get analog values like joysticks and mouse movement:
CSHARP
// Get movement input (WASD or left stick) - normalized Vector3
Vector3 moveInput = Input.AnalogMove;
// Get look input (mouse or right stick)
Vector2 lookInput = Input.AnalogLook;
// Get raw analog values
float forward = Input.AnalogMove.x;
float right = Input.AnalogMove.y;Mouse Input
CSHARP
// Get mouse wheel delta
Vector2 wheel = Input.MouseWheel;
// Check mouse buttons directly
if ( Input.MouseLeft ) { }
if ( Input.MouseRight ) { }
if ( Input.MouseMiddle ) { }Mouse Position
CSHARP
// Get mouse position in screen coordinates (0,0 is top-left)
Vector2 mousePos = Mouse.Position;
// Check if mouse is over a specific UI panel
if ( MyPanel.IsInside( Mouse.Position ) )
{
// Mouse is over this panel
}Custom Keys
You can check for specific keys directly:
CSHARP
if ( Input.Down( "slot1" ) ) SelectWeapon( 0 );
if ( Input.Down( "slot2" ) ) SelectWeapon( 1 );
if ( Input.Down( "slot3" ) ) SelectWeapon( 2 );Escape Key
The escape key has special handling for menus:
CSHARP
// Check if escape was pressed
if ( Input.EscapePressed )
{
TogglePauseMenu();
}
// Or absorb it to prevent default behavior
if ( Input.AbsorbKey( "escape" ) )
{
// Menu handled escape, don't pass to game
}Defining Input Actions
Input actions are defined in your project's .sbproj file under the Input section. This allows players to rebind keys and provides automatic gamepad support.
Common default actions include:
Was this helpful?