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
| Category | Keys |
|---|---|
| Letters | A through Z |
| Numbers | D0 through D9 |
| Function | F1 through F12 |
| Arrow | Up, Down, Left, Right |
| Modifiers | LeftShift, RightShift, LeftControl, RightControl, LeftAlt, RightAlt |
| Special | Space, Enter, Escape, Tab, Backspace |
| Navigation | Delete, 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();