Keyboard Input

Handle keyboard input using the RaylibInput static class.

using Xengine.Rendering;

Methods

IsKeyDown

static bool IsKeyDown(KeyCode key)

Returns true while the key is held down.

if (RaylibInput.IsKeyDown(KeyCode.W))
    Transform.Position += Vector3.Forward * speed * Time.Delta;

IsKeyPressed

static bool IsKeyPressed(KeyCode key)

Returns true only on the frame the key was pressed.

if (RaylibInput.IsKeyPressed(KeyCode.Space))
    Jump();

IsKeyReleased

static bool IsKeyReleased(KeyCode key)

Returns true only on the frame the key was released.

if (RaylibInput.IsKeyReleased(KeyCode.LeftShift))
    EndSprint();

IsAnyKeyPressed

static bool IsAnyKeyPressed()

Returns true if any key was pressed this frame.

Key Codes

CategoryKeys
LettersA through Z
NumbersD0 through D9
FunctionF1 through F12
ArrowUp, Down, Left, Right
ModifiersLeftShift, RightShift, LeftControl, RightControl, LeftAlt, RightAlt
SpecialSpace, Enter, Escape, Tab, Backspace
NavigationDelete, Insert, Home, End, PageUp, PageDown

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)
    {
        move = move.Normalized * Speed * Time.Delta;
        Transform.Position += move;
    }
}

Jump with Cooldown

private bool _canJump = true;

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

Sprint with Shift

protected override void OnUpdate()
{
    float speed = BaseSpeed;
    if (RaylibInput.IsKeyDown(KeyCode.LeftShift))
        speed = SprintSpeed;
    
    // Apply movement...
}

Menu Navigation

if (RaylibInput.IsKeyPressed(KeyCode.Escape))
    TogglePauseMenu();

if (RaylibInput.IsKeyPressed(KeyCode.Up))
    SelectPreviousOption();
    
if (RaylibInput.IsKeyPressed(KeyCode.Down))
    SelectNextOption();
    
if (RaylibInput.IsKeyPressed(KeyCode.Enter))
    ConfirmSelection();