RaylibInput

Keyboard and mouse input handling.

namespace Xengine.Rendering;
public static class RaylibInput

Keyboard Input

// Check if key is currently held down
if (RaylibInput.IsKeyDown(KeyCode.W))
{
    // Move forward while W is held
    Transform.Position += Vector3.Forward * speed * Time.Delta;
}

// Check if key was just pressed this frame
if (RaylibInput.IsKeyPressed(KeyCode.Space))
{
    // Jump (only triggers once per press)
    Jump();
}

// Check if key was just released
if (RaylibInput.IsKeyReleased(KeyCode.E))
{
    // Interaction ended
    EndInteraction();
}

Available Key Codes

LettersKeyCode.A through KeyCode.Z
NumbersKeyCode.D0 through KeyCode.D9
FunctionKeyCode.F1 through KeyCode.F12
ArrowsKeyCode.Up, Down, Left, Right
ModifiersKeyCode.LeftShift, RightShift, LeftControl, etc.
SpecialKeyCode.Space, Enter, Escape, Tab, Backspace

Mouse Input

// Get mouse position
Vector2 mousePos = RaylibInput.MousePosition;

// Get mouse movement since last frame
Vector2 mouseDelta = RaylibInput.MouseDelta;

// Get scroll wheel
float scroll = RaylibInput.MouseWheel;

// Mouse buttons (0=left, 1=right, 2=middle)
if (RaylibInput.IsMouseButtonDown(0))
{
    // Left mouse held
}

if (RaylibInput.IsMouseButtonPressed(1))
{
    // Right mouse just clicked
}

Common Patterns

WASD Movement

protected override void OnUpdate()
{
    var move = Vector3.Zero;
    
    if (RaylibInput.IsKeyDown(KeyCode.W)) move.Z += 1;
    if (RaylibInput.IsKeyDown(KeyCode.S)) move.Z -= 1;
    if (RaylibInput.IsKeyDown(KeyCode.A)) move.X -= 1;
    if (RaylibInput.IsKeyDown(KeyCode.D)) move.X += 1;
    
    if (move.LengthSquared > 0)
    {
        Transform.Position += move.Normalized * Speed * Time.Delta;
    }
}

Jump with Cooldown

private bool _canJump = true;

protected override void OnUpdate()
{
    if (RaylibInput.IsKeyPressed(KeyCode.Space) && _canJump)
    {
        _canJump = false;
        _velocityY = JumpForce;
    }
    
    // Reset when grounded
    if (_grounded) _canJump = true;
}

Mouse Look

protected override void OnUpdate()
{
    var delta = RaylibInput.MouseDelta;
    
    _yaw += delta.X * Sensitivity;
    _pitch -= delta.Y * Sensitivity;
    _pitch = MathHelper.Clamp(_pitch, -89f, 89f);
    
    // Apply rotation...
}

Action Key Check

protected override void OnUpdate()
{
    // Escape to pause
    if (RaylibInput.IsKeyPressed(KeyCode.Escape))
    {
        TogglePause();
    }
    
    // R to restart
    if (RaylibInput.IsKeyPressed(KeyCode.R))
    {
        RestartLevel();
    }
}