Skip to content

Commit

Permalink
Add received character (#805)
Browse files Browse the repository at this point in the history
* Add ReceivedCharacter window event

* Add ReceivedCharacter window event examples
  • Loading branch information
Jerald committed Nov 7, 2020
1 parent 9224983 commit f547885
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ path = "examples/input/keyboard_input.rs"
name = "keyboard_input_events"
path = "examples/input/keyboard_input_events.rs"

[[example]]
name = "char_input_events"
path = "examples/input/char_input_events.rs"

[[example]]
name = "gamepad_input"
path = "examples/input/gamepad_input.rs"
Expand Down
7 changes: 7 additions & 0 deletions crates/bevy_window/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,10 @@ pub struct CursorMoved {
pub id: WindowId,
pub position: Vec2,
}

/// An event that is sent whenever a window receives a character from the OS or underlying system.
#[derive(Debug, Clone)]
pub struct ReceivedCharacter {
pub id: WindowId,
pub char: char,
}
3 changes: 2 additions & 1 deletion crates/bevy_window/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub use window::*;
pub use windows::*;

pub mod prelude {
pub use crate::{CursorMoved, Window, WindowDescriptor, Windows};
pub use crate::{CursorMoved, ReceivedCharacter, Window, WindowDescriptor, Windows};
}

use bevy_app::prelude::*;
Expand Down Expand Up @@ -37,6 +37,7 @@ impl Plugin for WindowPlugin {
.add_event::<WindowCloseRequested>()
.add_event::<CloseWindow>()
.add_event::<CursorMoved>()
.add_event::<ReceivedCharacter>()
.init_resource::<Windows>();

if self.add_primary_window {
Expand Down
17 changes: 16 additions & 1 deletion crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use bevy_app::{prelude::*, AppExit};
use bevy_ecs::{IntoThreadLocalSystem, Resources, World};
use bevy_math::Vec2;
use bevy_window::{
CreateWindow, CursorMoved, Window, WindowCloseRequested, WindowCreated, WindowResized, Windows,
CreateWindow, CursorMoved, ReceivedCharacter, Window, WindowCloseRequested, WindowCreated,
WindowResized, Windows,
};
use winit::{
event::{self, DeviceEvent, Event, WindowEvent},
Expand Down Expand Up @@ -272,6 +273,20 @@ pub fn winit_runner(mut app: App) {
}
touch_input_events.send(converters::convert_touch_input(touch));
}
WindowEvent::ReceivedCharacter(c) => {
let mut char_input_events = app
.resources
.get_mut::<Events<ReceivedCharacter>>()
.unwrap();

let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();

char_input_events.send(ReceivedCharacter {
id: window_id,
char: c,
})
}
_ => {}
},
event::Event::DeviceEvent { ref event, .. } => {
Expand Down
23 changes: 23 additions & 0 deletions examples/input/char_input_events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use bevy::{prelude::*, window::ReceivedCharacter};

fn main() {
App::build()
.add_plugins(DefaultPlugins)
.add_system(print_char_event_system.system())
.run();
}

#[derive(Default)]
struct State {
event_reader: EventReader<ReceivedCharacter>,
}

/// This system prints out all char events as they come in
fn print_char_event_system(
mut state: Local<State>,
char_input_events: Res<Events<ReceivedCharacter>>,
) {
for event in state.event_reader.iter(&char_input_events) {
println!("{:?}: '{}'", event, event.char);
}
}

0 comments on commit f547885

Please sign in to comment.