use bevy::input::keyboard::KeyboardInput; use bevy::input::ButtonState; use bevy::prelude::*; fn main() { App::new() .add_plugins(DefaultPlugins) .add_systems(Startup, init) .add_systems(Update, update) .run(); } fn init(mut commands: Commands) { // UI Camera commands.spawn(( Camera2dBundle { ..default() }, UiCameraConfig { show_ui: true }, )); // Text Box commands.spawn(TextBundle::from_section( "Start typing! (Escape to clear)\n", TextStyle { color: Color::WHITE, ..default() }, )); } fn update(mut query: Query<&mut Text>, mut events: EventReader) { for KeyboardInput { key_code, scan_code, state, .. } in events.iter() { if *state == ButtonState::Pressed { let mut text = query.single_mut(); if *key_code == Some(KeyCode::Escape) { text.sections.drain(1..); } else { let style = TextStyle { color: Color::WHITE, ..default() }; let added = format!( "Pressed: KeyCode: {:?} / ScanCode {:?}\n", key_code, scan_code ); text.sections.push(TextSection::new(added, style)); } } } }