diff --git a/crates/bevy_input/src/keyboard.rs b/crates/bevy_input/src/keyboard.rs index a792bc0f4b4df..a499b62524866 100644 --- a/crates/bevy_input/src/keyboard.rs +++ b/crates/bevy_input/src/keyboard.rs @@ -1,4 +1,5 @@ use crate::{ButtonState, Input}; +use bevy_ecs::entity::Entity; use bevy_ecs::{change_detection::DetectChangesMut, event::EventReader, system::ResMut}; use bevy_reflect::{FromReflect, Reflect}; @@ -28,6 +29,8 @@ pub struct KeyboardInput { pub key_code: Option, /// The press state of the key. pub state: ButtonState, + /// Window that received the input. + pub window: Entity, } /// Updates the [`Input`] resource with the latest [`KeyboardInput`] events. diff --git a/crates/bevy_input/src/mouse.rs b/crates/bevy_input/src/mouse.rs index 9fbac1ce937ff..6a771a10f1136 100644 --- a/crates/bevy_input/src/mouse.rs +++ b/crates/bevy_input/src/mouse.rs @@ -1,4 +1,5 @@ use crate::{ButtonState, Input}; +use bevy_ecs::entity::Entity; use bevy_ecs::{change_detection::DetectChangesMut, event::EventReader, system::ResMut}; use bevy_math::Vec2; use bevy_reflect::{FromReflect, Reflect}; @@ -26,6 +27,8 @@ pub struct MouseButtonInput { pub button: MouseButton, /// The pressed state of the button. pub state: ButtonState, + /// Window that received the input. + pub window: Entity, } /// A button on a mouse device. @@ -120,6 +123,8 @@ pub struct MouseWheel { pub x: f32, /// The vertical scroll value. pub y: f32, + /// Window that received the input. + pub window: Entity, } /// Updates the [`Input`] resource with the latest [`MouseButtonInput`] events. diff --git a/crates/bevy_winit/src/converters.rs b/crates/bevy_winit/src/converters.rs index 28301f48da520..aca9f4ba30f14 100644 --- a/crates/bevy_winit/src/converters.rs +++ b/crates/bevy_winit/src/converters.rs @@ -1,3 +1,4 @@ +use bevy_ecs::entity::Entity; use bevy_input::{ keyboard::{KeyCode, KeyboardInput}, mouse::MouseButton, @@ -7,11 +8,15 @@ use bevy_input::{ use bevy_math::Vec2; use bevy_window::{CursorIcon, WindowLevel}; -pub fn convert_keyboard_input(keyboard_input: &winit::event::KeyboardInput) -> KeyboardInput { +pub fn convert_keyboard_input( + keyboard_input: &winit::event::KeyboardInput, + window: Entity, +) -> KeyboardInput { KeyboardInput { scan_code: keyboard_input.scancode, state: convert_element_state(keyboard_input.state), key_code: keyboard_input.virtual_keycode.map(convert_virtual_key_code), + window, } } diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index caf74d3eeb0d8..18cb5b41543ba 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -419,7 +419,7 @@ pub fn winit_runner(mut app: App) { WindowEvent::KeyboardInput { ref input, .. } => { input_events .keyboard_input - .send(converters::convert_keyboard_input(input)); + .send(converters::convert_keyboard_input(input, window_entity)); } WindowEvent::CursorMoved { position, .. } => { let physical_position = DVec2::new( @@ -452,6 +452,7 @@ pub fn winit_runner(mut app: App) { input_events.mouse_button_input.send(MouseButtonInput { button: converters::convert_mouse_button(button), state: converters::convert_element_state(state), + window: window_entity, }); } WindowEvent::MouseWheel { delta, .. } => match delta { @@ -460,6 +461,7 @@ pub fn winit_runner(mut app: App) { unit: MouseScrollUnit::Line, x, y, + window: window_entity, }); } event::MouseScrollDelta::PixelDelta(p) => { @@ -467,6 +469,7 @@ pub fn winit_runner(mut app: App) { unit: MouseScrollUnit::Pixel, x: p.x as f32, y: p.y as f32, + window: window_entity, }); } },