Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Input

Input in raylib-rs is queried per-frame off RaylibHandle — there is no callback or event queue. Every call snapshots the state that raylib collected during the previous EndDrawing call. The API covers keyboard, mouse, gamepad, and touch in a uniform style: is_*_down for level queries and is_*_pressed/is_*_released for edge-triggered queries.

API surface

Example

extern crate raylib;
use raylib::prelude::*;

fn main() {
    let (mut rl, thread) = raylib::init()
        .size(640, 480)
        .title("Input demo")
        .build();

    while !rl.window_should_close() {
        // Level query — true every frame the key is held.
        if rl.is_key_down(KeyboardKey::KEY_SPACE) {
            println!("SPACE held");
        }

        // Edge query — true only on the first frame of a press.
        if rl.is_key_pressed(KeyboardKey::KEY_ENTER) {
            println!("ENTER pressed");
        }

        let mouse = rl.get_mouse_position();
        let scroll = rl.get_mouse_wheel_move();

        let mut d = rl.begin_drawing(&thread);
        d.clear_background(Color::RAYWHITE);
        d.draw_text(
            &format!("mouse: ({:.0}, {:.0})  scroll: {:.1}", mouse.x, mouse.y, scroll),
            10, 10, 20, Color::DARKGRAY,
        );
    }
}

Gotchas

  • is_key_pressed is per-frame edge-triggered. It returns true only on the single frame where the key transitions from up to down. If you poll it every frame and hold the key, you will see exactly one true unless you also check is_key_pressed_repeat for held-down auto-repeat.
  • Mouse coordinates are Y-down. get_mouse_position().y increases downward, which matches window pixel conventions but is the opposite of mathematical Y-up.
  • get_gamepad_button_pressed transmute UB (tracked-deferred). The current implementation performs a transmute::<u32, GamepadButton> on the raw integer returned by raylib. If raylib returns a value outside the known GamepadButton variants, this is undefined behaviour. A soundness fix is tracked for a future PR. For now, rely only on is_gamepad_button_pressed / is_gamepad_button_down with explicit GamepadButton variants.

See also

Showcase examples

Showcase examples that exercise this module: