terminalCode Example
Input Handling Code Examples
Input Handling Code Examples
Comprehensive input examples covering buttons, analog sticks, mouse, controller, and VR input.
CSHARP
// Input Handling Examples
using Sandbox;
public class InputExamples : Component
{
// ==================== BUTTON INPUT ====================
void ButtonInput()
{
// Check if button is held down
if (Input.Down("jump"))
{
// Button is currently pressed
}
// Check if button was just pressed this frame
if (Input.Pressed("attack1"))
{
// Single fire on press
FireWeapon();
}
// Check if button was just released
if (Input.Released("reload"))
{
// Action on release (e.g., charge-up attacks)
}
// Case insensitive
Input.Down("JUMP") == Input.Down("jump"); // true
}
// ==================== ANALOG INPUT ====================
void AnalogInput()
{
// Movement input (WASD or left stick)
Vector3 moveInput = Input.AnalogMove;
// Returns: Vector3(x, y, 0) where x=right/left, y=forward/back
// Look input (mouse or right stick)
Vector3 lookInput = Input.AnalogLook;
// Returns: Vector3(yaw, pitch, 0)
// Apply to transform
var wishDir = new Vector3(moveInput.x, moveInput.y, 0);
WorldPosition += wishDir * Speed * Time.Delta;
// Rotate based on look
var newRotation = Rotation.FromYawPitchRoll(
lookInput.x * Sensitivity,
lookInput.y * Sensitivity,
0
);
WorldRotation = newRotation;
}
// ==================== MOUSE INPUT ====================
void MouseInput()
{
// Raw mouse delta this frame
Vector2 mouseDelta = Input.MouseDelta;
// Mouse position in screen coordinates
Vector2 mousePos = Input.MousePosition;
// Ray from camera through mouse position
Ray mouseRay = Scene.Camera.ScreenPixelToRay(mousePos);
// Trace under mouse
var tr = Scene.Trace.Ray(mouseRay, 5000).Run();
if (tr.Hit)
{
Log.Info($"Mouse over: {tr.GameObject}");
}
}
// ==================== CUSTOM INPUT ACTIONS ====================
void CustomActions()
{
// Define in Project Settings -> Input
// Or create at runtime:
// Using custom action names
if (Input.Pressed("use")) Interact();
if (Input.Down("sprint")) Sprint();
if (Input.Released("crouch")) StandUp();
// Weapon switching
if (Input.Pressed("slot1")) EquipWeapon(0);
if (Input.Pressed("slot2")) EquipWeapon(1);
if (Input.Pressed("slot3")) EquipWeapon(2);
}
// ==================== CONTROLLER INPUT ====================
void ControllerInput()
{
// Check if using controller
bool isController = Input.UsingController;
// Vibration/rumble
Input.Controller.Vibrate(leftMotor: 0.5f, rightMotor: 0.3f, duration: 0.2f);
// Specific controller buttons
if (Input.Down("gamepad_a")) Jump();
if (Input.Down("gamepad_b")) Cancel();
if (Input.Down("gamepad_x")) Reload();
if (Input.Down("gamepad_y")) Use();
// Triggers
float leftTrigger = Input.Down("left_trigger") ? 1 : 0;
float rightTrigger = Input.Down("right_trigger") ? 1 : 0;
// Bumpers
if (Input.Pressed("left_bumper")) PreviousWeapon();
if (Input.Pressed("right_bumper")) NextWeapon();
// D-Pad
if (Input.Pressed("dpad_up")) MenuUp();
if (Input.Pressed("dpad_down")) MenuDown();
}
// ==================== VR INPUT ====================
void VRInput()
{
if (!Input.VR.IsActive) return;
// VR controller input
Vector3 leftJoystick = Input.VR.LeftHand.Joystick;
float leftTrigger = Input.VR.LeftHand.Trigger;
bool leftGrip = Input.VR.LeftHand.Grip > 0.5f;
// Haptic feedback
Input.VR.LeftHand.Haptics.Vibrate(0.5f, 0.1f);
// Button presses
if (Input.VR.LeftHand.ButtonA) Teleport();
if (Input.VR.LeftHand.ButtonB) Menu();
}
// ==================== INPUT GLYPHS ====================
void ShowInputGlyphs()
{
// Get icon/glyph for current input device
var jumpGlyph = Input.GetGlyph("jump");
// Returns appropriate icon: "Space" for keyboard, "A" button for controller
// Display in UI
Label.Text = $"Press {jumpGlyph} to jump";
}
// ==================== ESCAPE KEY OVERRIDE ====================
void HandleEscapeKey()
{
// Override default pause menu behavior
if (Input.EscapePressed)
{
Input.EscapePressed = false; // Prevent default pause menu
// Show custom menu
ToggleCustomPauseMenu();
}
}
}Default Input Actions
| Action | Default Binding |
|---|---|
| forward | W / Up / Left Stick Up |
| back | S / Down / Left Stick Down |
| left | A / Left / Left Stick Left |
| right | D / Right / Left Stick Right |
| jump | Space / A Button |
| duck | Ctrl / B Button |
| sprint | Shift / Left Stick Click |
| attack1 | Mouse1 / Right Trigger |
| attack2 | Mouse2 / Left Trigger |
| reload | R / X Button |
| use | E / Y Button |
| slot1-6 | 1-6 / D-Pad |
| menu | Q / Start Button |
| score | Tab / Select Button |
| chat | Enter / Right Menu |
| voice | V / Left Grip |
Was this helpful?