book/src/modules/input.md
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.
RaylibHandle::is_key_down —
true while a key is held.RaylibHandle::is_key_pressed —
true on the first frame a key transitions to down (edge-triggered).RaylibHandle::get_mouse_position —
returns a Vector2 in window pixels, Y-down.RaylibHandle::get_mouse_wheel_move —
returns scroll delta for the current frame.RaylibHandle::is_gamepad_available —
checks whether a gamepad index is connected.RaylibHandle::get_gamepad_button_pressed —
returns Option<GamepadButton> for the most recently pressed button.KeyboardKey —
enum of all keyboard scancodes (e.g., KeyboardKey::KEY_SPACE).MouseButton —
left / right / middle and extended buttons.GamepadButton —
cross-platform gamepad button enum.# 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,
);
}
}
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.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.KeyboardKey docs.rs —
full list of key constants.Showcase examples that exercise this module: