bevy/examples/input/keyboard_input.rs
Kristoffer Søholm 2119838e27
Add support for ButtonInput<Key> (#19684)
# Objective

While `KeyCode` is very often the correct way to interact with keyboard
input there are a bunch of cases where it isn't, notably most of the
symbols (e.g. plus, minus, different parentheses). Currently the only
way to get these is to read from `EventReader<KeyboardInput>`, but then
you'd have to redo the `ButtonInput` logic for pressed/released to e.g.
make zoom functionality that depends on plus/minus keys.

This has led to confusion previously, like
https://github.com/bevyengine/bevy/issues/3278

## Solution

Add a `ButtonInput<Key>` resource.

## Testing

Modified the `keyboard_input` example to test it.

## Open questions

I'm not 100% sure this is the right way forward, since it duplicates the
key processing logic and might make people use the shorter
`ButtonInput<Key>` even when it's not appropriate.

Another option is to add a new struct with both `Key` and `KeyCode`, and
use `ButtonInput` with that instead. That would make it more
explanatory, but that is a lot of churn.

The third alternative is to not do this because it's too niche.

I'll add more documentation and take it out of draft if we want to move
forward with it.
2025-06-18 20:15:03 +00:00

44 lines
1.3 KiB
Rust

//! Demonstrates handling a key press/release.
use bevy::{input::keyboard::Key, prelude::*};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Update, keyboard_input_system)
.run();
}
/// This system responds to certain key presses
fn keyboard_input_system(
keyboard_input: Res<ButtonInput<KeyCode>>,
key_input: Res<ButtonInput<Key>>,
) {
// KeyCode is used when you want the key location across different keyboard layouts
// See https://w3c.github.io/uievents-code/#code-value-tables for the locations
if keyboard_input.pressed(KeyCode::KeyA) {
info!("'A' currently pressed");
}
if keyboard_input.just_pressed(KeyCode::KeyA) {
info!("'A' just pressed");
}
if keyboard_input.just_released(KeyCode::KeyA) {
info!("'A' just released");
}
// Key is used when you want a specific key, no matter where it is located.
// This is useful for symbols that have a specific connotation, e.g. '?' for
// a help menu or '+'/'-' for zoom
let key = Key::Character("?".into());
if key_input.pressed(key.clone()) {
info!("'?' currently pressed");
}
if key_input.just_pressed(key.clone()) {
info!("'?' just pressed");
}
if key_input.just_released(key) {
info!("'?' just released");
}
}