Skip to content

Input System

MichaelFisher1997 edited this page Jan 5, 2026 · 1 revision

Input System

Keyboard and mouse input handling via SDL3 event polling.


Overview

File: src/engine/input/input.zig

The input system polls SDL3 events each frame and tracks key/mouse state for gameplay input.


Input Struct

pub const Input = struct {
    // Keyboard
    keys_down: AutoHashMap(Key, void),      // Currently held
    keys_pressed: AutoHashMap(Key, void),   // Pressed this frame
    keys_released: AutoHashMap(Key, void),  // Released this frame
    
    // Mouse
    mouse_buttons: [8]bool,
    mouse_buttons_pressed: [8]bool,
    mouse_buttons_released: [8]bool,
    mouse_x: i32, mouse_y: i32,
    mouse_dx: i32, mouse_dy: i32,           // Delta (relative)
    scroll_x: f32, scroll_y: f32,
    
    // Window
    window_width: u32, window_height: u32,
    should_quit: bool,
    mouse_captured: bool,
};

Frame Lifecycle

// Each frame:
input.beginFrame();   // Clear per-frame state (pressed/released)
input.pollEvents();   // Process SDL event queue

Query Methods

Keyboard

Method Description
isKeyDown(key) Key currently held
isKeyPressed(key) Key pressed this frame
isKeyReleased(key) Key released this frame

Mouse

Method Description
isMouseButtonDown(button) Button currently held
isMouseButtonPressed(button) Button clicked this frame
isMouseButtonReleased(button) Button released this frame
getMousePosition() Absolute screen position
getMouseDelta() Relative motion since last frame
getScrollDelta() Scroll wheel delta

Key Enum

Comprehensive key definitions:

Letters

a through z

Numbers

n0 through n9

Navigation

Key Description
up, down, left, right Arrow keys
home, end Home/End
page_up, page_down Page navigation

Modifiers

Key Description
shift Shift key
ctrl Control key
alt Alt key

Function Keys

f1 through f12

Special

Key Description
escape Escape
space Space bar
tab Tab key
backspace Backspace
enter Enter/Return
insert, delete Insert/Delete

Symbols

minus, equals, left_bracket, right_bracket, backslash, semicolon, apostrophe, grave, comma, period, slash


Mouse Capture

For FPS-style mouse look:

input.setMouseCapture(window, true);   // Hide cursor, relative mode
input.setMouseCapture(window, false);  // Restore cursor

When captured:

  • Cursor is hidden
  • Mouse uses relative mode (delta only)
  • Cursor cannot leave window

Event Processing

Handles SDL3 events:

SDL Event Action
SDL_EVENT_QUIT Set should_quit = true
SDL_EVENT_KEY_DOWN Add to keys_down and keys_pressed
SDL_EVENT_KEY_UP Remove from keys_down, add to keys_released
SDL_EVENT_MOUSE_MOTION Update position and delta
SDL_EVENT_MOUSE_BUTTON_DOWN/UP Update button state
SDL_EVENT_MOUSE_WHEEL Update scroll delta
SDL_EVENT_WINDOW_RESIZED Update window dimensions

Key Repeat

Key repeat events are ignored (only initial press registers as "pressed").

HiDPI Support

Window resize uses pixel size for proper HiDPI scaling.


Usage Example

const input = try Input.init(allocator);
defer input.deinit();

// Game loop
while (!input.should_quit) {
    input.beginFrame();
    input.pollEvents();
    
    // Movement
    if (input.isKeyDown(.w)) player.moveForward();
    if (input.isKeyDown(.s)) player.moveBackward();
    
    // One-shot actions
    if (input.isKeyPressed(.space)) player.jump();
    if (input.isKeyPressed(.escape)) toggleMenu();
    
    // Mouse look
    const delta = input.getMouseDelta();
    camera.rotate(delta.x * sensitivity, delta.y * sensitivity);
    
    // Scroll
    if (input.scroll_y != 0) {
        inventory.scrollHotbar(@intFromFloat(input.scroll_y));
    }
}

Game Controls Reference

Key Action
W/A/S/D Movement
SPACE Jump / Fly up
SHIFT Fly down
ESC Pause menu
TAB Toggle mouse capture
F Toggle wireframe
V Toggle VSync
F2 Toggle FPS counter
F3 Toggle creative/survival
I Open inventory
M Open map
1-9 Select hotbar slot
Left Click Break block
Right Click Place block
Scroll Scroll hotbar

See Also


Source: src/engine/input/input.zig | Last updated: January 2026

Clone this wiki locally