bevy/examples/input/char_input_events.rs
Antony 7b4b5966d9
Deprecate ReceivedCharacter (#12868)
# Objective

- Partially resolves #12639.

## Solution

- Deprecate `ReceivedCharacter`.
- Replace `ReceivedCharacter` with `KeyboardInput` in the relevant
examples.

## Migration Guide

- `ReceivedCharacter` is now deprecated, use `KeyboardInput` instead.

- Before:
  ```rust
  fn listen_characters(events: EventReader<ReceivedCharacter>) {
    for event in events.read() {
      info!("{}", event.char);
    }
  }
  ```
  
  After:
  ```rust
  fn listen_characters(events: EventReader<KeyboardInput>) {
    for event in events.read() {
      // Only check for characters when the key is pressed.
      if event.state == ButtonState::Released {
        continue;
      }
// Note that some keys such as `Space` and `Tab` won't be detected as
before.
      // Instead, check for them with `Key::Space` and `Key::Tab`.
      if let Key::Character(character) = &event.logical_key {
        info!("{}", character);
      }
    }
  }
  ```

---------

Co-authored-by: Mike <mike.hsu@gmail.com>
2024-04-30 00:49:41 +00:00

29 lines
757 B
Rust

//! Prints out all chars as they are inputted.
use bevy::{
input::{
keyboard::{Key, KeyboardInput},
ButtonState,
},
prelude::*,
};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Update, print_char_event_system)
.run();
}
/// This system prints out all char events as they come in
fn print_char_event_system(mut char_input_events: EventReader<KeyboardInput>) {
for event in char_input_events.read() {
// Only check for characters when the key is pressed
if event.state == ButtonState::Released {
continue;
}
if let Key::Character(character) = &event.logical_key {
info!("{:?}: '{}'", event, character);
}
}
}