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>
This commit is contained in:
Antony 2024-04-29 20:49:41 -04:00 committed by GitHub
parent d9b69731de
commit 7b4b5966d9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 44 additions and 19 deletions

View file

@ -1,3 +1,4 @@
#![allow(deprecated)]
use std::path::PathBuf;
use bevy_ecs::entity::Entity;
@ -171,6 +172,7 @@ pub struct CursorLeft {
}
/// An event that is sent whenever a window receives a character from the OS or underlying system.
#[deprecated(since = "0.14.0", note = "Use `KeyboardInput` instead.")]
#[derive(Event, Debug, Clone, PartialEq, Eq, Reflect)]
#[reflect(Debug, PartialEq)]
#[cfg_attr(

View file

@ -28,6 +28,7 @@ pub use window::*;
#[allow(missing_docs)]
pub mod prelude {
#[allow(deprecated)]
#[doc(hidden)]
pub use crate::{
CursorEntered, CursorIcon, CursorLeft, CursorMoved, FileDragAndDrop, Ime, MonitorSelection,
@ -85,6 +86,7 @@ pub struct WindowPlugin {
impl Plugin for WindowPlugin {
fn build(&self, app: &mut App) {
// User convenience events
#[allow(deprecated)]
app.add_event::<WindowResized>()
.add_event::<WindowCreated>()
.add_event::<WindowClosed>()
@ -132,6 +134,7 @@ impl Plugin for WindowPlugin {
}
// Register event types
#[allow(deprecated)]
app.register_type::<WindowResized>()
.register_type::<RequestRedraw>()
.register_type::<WindowCreated>()

View file

@ -43,6 +43,7 @@ use bevy_math::{ivec2, DVec2, Vec2};
#[cfg(not(target_arch = "wasm32"))]
use bevy_tasks::tick_global_task_pools_on_main_thread;
use bevy_utils::tracing::{error, trace, warn};
#[allow(deprecated)]
use bevy_window::{
exit_on_all_closed, ApplicationLifetime, CursorEntered, CursorLeft, CursorMoved,
FileDragAndDrop, Ime, ReceivedCharacter, RequestRedraw, Window,
@ -474,6 +475,7 @@ fn handle_winit_event(
if event.state.is_pressed() {
if let Some(char) = &event.text {
let char = char.clone();
#[allow(deprecated)]
winit_events.send(ReceivedCharacter { window, char });
}
}

View file

@ -1,3 +1,4 @@
#![allow(deprecated)]
#![allow(missing_docs)]
use bevy_app::App;

View file

@ -1,6 +1,12 @@
//! Prints out all chars as they are inputted.
use bevy::prelude::*;
use bevy::{
input::{
keyboard::{Key, KeyboardInput},
ButtonState,
},
prelude::*,
};
fn main() {
App::new()
@ -10,8 +16,14 @@ fn main() {
}
/// This system prints out all char events as they come in
fn print_char_event_system(mut char_input_events: EventReader<ReceivedCharacter>) {
fn print_char_event_system(mut char_input_events: EventReader<KeyboardInput>) {
for event in char_input_events.read() {
info!("{:?}: '{}'", event, event.char);
// 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);
}
}
}

View file

@ -6,7 +6,13 @@
use std::mem;
use bevy::{input::keyboard::KeyboardInput, prelude::*};
use bevy::{
input::{
keyboard::{Key, KeyboardInput},
ButtonState,
},
prelude::*,
};
fn main() {
App::new()
@ -17,7 +23,6 @@ fn main() {
(
toggle_ime,
listen_ime_events,
listen_received_character_events,
listen_keyboard_input_events,
bubbling_text,
),
@ -163,25 +168,19 @@ fn listen_ime_events(
}
}
fn listen_received_character_events(
mut events: EventReader<ReceivedCharacter>,
mut edit_text: Query<&mut Text, (Without<Node>, Without<Bubble>)>,
) {
for event in events.read() {
edit_text.single_mut().sections[0]
.value
.push_str(&event.char);
}
}
fn listen_keyboard_input_events(
mut commands: Commands,
mut events: EventReader<KeyboardInput>,
mut edit_text: Query<&mut Text, (Without<Node>, Without<Bubble>)>,
) {
for event in events.read() {
match event.key_code {
KeyCode::Enter => {
// Only trigger changes when the key is first pressed.
if event.state == ButtonState::Released {
continue;
}
match &event.logical_key {
Key::Enter => {
let mut text = edit_text.single_mut();
if text.sections[0].value.is_empty() {
continue;
@ -198,9 +197,15 @@ fn listen_keyboard_input_events(
},
));
}
KeyCode::Backspace => {
Key::Space => {
edit_text.single_mut().sections[0].value.push(' ');
}
Key::Backspace => {
edit_text.single_mut().sections[0].value.pop();
}
Key::Character(character) => {
edit_text.single_mut().sections[0].value.push_str(character);
}
_ => continue,
}
}