Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Input System
MichaelFisher1997 edited this page Jan 5, 2026
·
1 revision
Keyboard and mouse input handling via SDL3 event polling.
File:src/engine/input/input.zig
The input system polls SDL3 events each frame and tracks key/mouse state for gameplay input.
pubconstInput=struct {
// Keyboardkeys_down: AutoHashMap(Key, void), // Currently heldkeys_pressed: AutoHashMap(Key, void), // Pressed this framekeys_released: AutoHashMap(Key, void), // Released this frame// Mousemouse_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,
// Windowwindow_width: u32, window_height: u32,
should_quit: bool,
mouse_captured: bool,
};// Each frame:input.beginFrame(); // Clear per-frame state (pressed/released)input.pollEvents(); // Process SDL event queue| Method | Description |
|---|---|
isKeyDown(key) | Key currently held |
isKeyPressed(key) | Key pressed this frame |
isKeyReleased(key) | Key released this frame |
| 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 |
Comprehensive key definitions:
a through z
n0 through n9
| Key | Description |
|---|---|
up, down, left, right | Arrow keys |
home, end | Home/End |
page_up, page_down | Page navigation |
| Key | Description |
|---|---|
shift | Shift key |
ctrl | Control key |
alt | Alt key |
f1 through f12
| Key | Description |
|---|---|
escape | Escape |
space | Space bar |
tab | Tab key |
backspace | Backspace |
enter | Enter/Return |
insert, delete | Insert/Delete |
minus, equals, left_bracket, right_bracket, backslash, semicolon, apostrophe, grave, comma, period, slash
For FPS-style mouse look:
input.setMouseCapture(window, true); // Hide cursor, relative modeinput.setMouseCapture(window, false); // Restore cursorWhen captured:
- Cursor is hidden
- Mouse uses relative mode (delta only)
- Cursor cannot leave window
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 events are ignored (only initial press registers as "pressed").
Window resize uses pixel size for proper HiDPI scaling.
constinput=tryInput.init(allocator);
deferinput.deinit();
// Game loopwhile (!input.should_quit) {
input.beginFrame();
input.pollEvents();
// Movementif (input.isKeyDown(.w)) player.moveForward();
if (input.isKeyDown(.s)) player.moveBackward();
// One-shot actionsif (input.isKeyPressed(.space)) player.jump();
if (input.isKeyPressed(.escape)) toggleMenu();
// Mouse lookconstdelta=input.getMouseDelta();
camera.rotate(delta.x*sensitivity, delta.y*sensitivity);
// Scrollif (input.scroll_y!=0) {
inventory.scrollHotbar(@intFromFloat(input.scroll_y));
}
}| 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 |
- Game Systems - Player controls
- Architecture - System overview
Source: src/engine/input/input.zig | Last updated: January 2026