Handling Input and Time
React to the player and the clock.
Input as a Resource
Keyboard state lives in the ButtonInput<KeyCode> resource, provided by DefaultPlugins.
Request it read-only with Res. Bevy updates it each frame before your systems run, so you always see the current state.
fn read_keys(keys: Res<ButtonInput<KeyCode>>) {
if keys.pressed(KeyCode::Space) {
info!("space held");
}
}pressed vs just_pressed
pressed is true every frame a key is held — ideal for continuous movement. just_pressed is true only on the frame the key goes down.
Use just_pressed for discrete actions like jumping or firing so a single tap fires once.
fn jump(keys: Res<ButtonInput<KeyCode>>) {
if keys.just_pressed(KeyCode::ArrowUp) {
info!("jump!");
}
}All lessons in this course
- The ECS Mindset
- Spawning and Moving Entities
- Handling Input and Time
- Collisions and Game State