
This was adopted from #12878. I rebased the changes resolved the following merge conflicts: - moved over the changes originally done in bevy_winit/src/lib.rs's `handle_winit_event` into bevy_winit/src/state.rs's `window_event` function - moved WinitEvent::KeyboardFocusLost event forwarding originally done in bevy_winit/src/winit_event.rs to the equivalent in bevy_winit/src/state.rs Tested this by following the modified keyboard_input example from the original PR. First, I verified I could reproduce the issue without the changes. Then, after applying the changes, I verified that when I Alt+Tabbed away from the running example that the log showed I released Alt and when I tabbed back it didn't behave like Alt was stuck. The following is from the original pull request by @gavlig # Objective This helps avoiding stuck key presses after switching from and back to Bevy window. Key press event gets stuck because window loses focus before receiving a key release event thus we end up with false positive in ButtonInput. ## Solution I saw two ways to fix this: 1. add bevy_window as dependency and read WindowFocus events 2. add a KeyboardFocusLost event specifically for this. I chose the latter because adding another dependency felt wrong, but if that is more preferable changing this pr won't be a problem. Also if someone sees another way please let me know. To test the bug use this small modification over examples/keyboard_input.rs: (it will work only if you have Alt-Tab combination for switching between windows in your OS, otherwise change AltLeft accordingly) ``` //! Demonstrates handling a key press/release. use bevy::{prelude::*, input:⌨️:KeyboardInput}; fn main() { App::new() .add_plugins(DefaultPlugins) .add_systems(Update, keyboard_input_system) .run(); } /// This system prints 'Alt' key state fn keyboard_input_system(keyboard_input: Res<ButtonInput<KeyCode>>, mut keyboard_input_events: EventReader<KeyboardInput>) { for event in keyboard_input_events.read() { info!("{:?}", event); } if keyboard_input.pressed(KeyCode::AltLeft) { info!("'Alt' currently pressed"); } if keyboard_input.just_pressed(KeyCode::AltLeft) { info!("'Alt' just pressed"); } if keyboard_input.just_released(KeyCode::AltLeft) { info!("'Alt' just released"); } } ``` Here i made a quick video with demo of the fix: https://youtu.be/qTvUCk4IHvo In first part i press Alt and Alt+Tab to switch back and forth from example app, logs will indicate that too. In second part I applied fix and you'll see that Alt will no longer be pressed when window gets unfocused ## Migration Guide `WinitEvent` has a new enum variant: `WinitEvent::KeyboardFocusLost`. Co-authored-by: gavlig <gavlig@gmail.com>
152 lines
5.0 KiB
Rust
152 lines
5.0 KiB
Rust
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
|
#![forbid(unsafe_code)]
|
|
#![doc(
|
|
html_logo_url = "https://bevyengine.org/assets/icon.png",
|
|
html_favicon_url = "https://bevyengine.org/assets/icon.png"
|
|
)]
|
|
|
|
//! Input functionality for the [Bevy game engine](https://bevyengine.org/).
|
|
//!
|
|
//! # Supported input devices
|
|
//!
|
|
//! `bevy` currently supports keyboard, mouse, gamepad, and touch inputs.
|
|
|
|
mod axis;
|
|
mod button_input;
|
|
/// Common run conditions
|
|
pub mod common_conditions;
|
|
pub mod gamepad;
|
|
pub mod gestures;
|
|
pub mod keyboard;
|
|
pub mod mouse;
|
|
pub mod touch;
|
|
|
|
pub use axis::*;
|
|
pub use button_input::*;
|
|
|
|
/// Most commonly used re-exported types.
|
|
pub mod prelude {
|
|
#[doc(hidden)]
|
|
pub use crate::{
|
|
gamepad::{
|
|
Gamepad, GamepadAxis, GamepadAxisType, GamepadButton, GamepadButtonType, Gamepads,
|
|
},
|
|
keyboard::KeyCode,
|
|
mouse::MouseButton,
|
|
touch::{TouchInput, Touches},
|
|
Axis, ButtonInput,
|
|
};
|
|
}
|
|
|
|
use bevy_app::prelude::*;
|
|
use bevy_ecs::prelude::*;
|
|
use bevy_reflect::Reflect;
|
|
use gestures::*;
|
|
use keyboard::{keyboard_input_system, KeyCode, KeyboardFocusLost, KeyboardInput};
|
|
use mouse::{mouse_button_input_system, MouseButton, MouseButtonInput, MouseMotion, MouseWheel};
|
|
use touch::{touch_screen_input_system, TouchInput, Touches};
|
|
|
|
use gamepad::{
|
|
gamepad_axis_event_system, gamepad_button_event_system, gamepad_connection_system,
|
|
gamepad_event_system, GamepadAxis, GamepadAxisChangedEvent, GamepadButton,
|
|
GamepadButtonChangedEvent, GamepadButtonInput, GamepadConnectionEvent, GamepadEvent,
|
|
GamepadRumbleRequest, GamepadSettings, Gamepads,
|
|
};
|
|
|
|
#[cfg(feature = "serialize")]
|
|
use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
|
|
|
|
/// Adds keyboard and mouse input to an App
|
|
#[derive(Default)]
|
|
pub struct InputPlugin;
|
|
|
|
/// Label for systems that update the input data.
|
|
#[derive(Debug, PartialEq, Eq, Clone, Hash, SystemSet)]
|
|
pub struct InputSystem;
|
|
|
|
impl Plugin for InputPlugin {
|
|
fn build(&self, app: &mut App) {
|
|
app
|
|
// keyboard
|
|
.add_event::<KeyboardInput>()
|
|
.add_event::<KeyboardFocusLost>()
|
|
.init_resource::<ButtonInput<KeyCode>>()
|
|
.add_systems(PreUpdate, keyboard_input_system.in_set(InputSystem))
|
|
// mouse
|
|
.add_event::<MouseButtonInput>()
|
|
.add_event::<MouseMotion>()
|
|
.add_event::<MouseWheel>()
|
|
.init_resource::<ButtonInput<MouseButton>>()
|
|
.add_systems(PreUpdate, mouse_button_input_system.in_set(InputSystem))
|
|
.add_event::<PinchGesture>()
|
|
.add_event::<RotationGesture>()
|
|
.add_event::<DoubleTapGesture>()
|
|
.add_event::<PanGesture>()
|
|
// gamepad
|
|
.add_event::<GamepadConnectionEvent>()
|
|
.add_event::<GamepadButtonChangedEvent>()
|
|
.add_event::<GamepadButtonInput>()
|
|
.add_event::<GamepadAxisChangedEvent>()
|
|
.add_event::<GamepadEvent>()
|
|
.add_event::<GamepadRumbleRequest>()
|
|
.init_resource::<GamepadSettings>()
|
|
.init_resource::<Gamepads>()
|
|
.init_resource::<ButtonInput<GamepadButton>>()
|
|
.init_resource::<Axis<GamepadAxis>>()
|
|
.init_resource::<Axis<GamepadButton>>()
|
|
.add_systems(
|
|
PreUpdate,
|
|
(
|
|
gamepad_event_system,
|
|
gamepad_connection_system.after(gamepad_event_system),
|
|
gamepad_button_event_system
|
|
.after(gamepad_event_system)
|
|
.after(gamepad_connection_system),
|
|
gamepad_axis_event_system
|
|
.after(gamepad_event_system)
|
|
.after(gamepad_connection_system),
|
|
)
|
|
.in_set(InputSystem),
|
|
)
|
|
// touch
|
|
.add_event::<TouchInput>()
|
|
.init_resource::<Touches>()
|
|
.add_systems(PreUpdate, touch_screen_input_system.in_set(InputSystem));
|
|
|
|
// Register common types
|
|
app.register_type::<ButtonState>()
|
|
.register_type::<KeyboardInput>()
|
|
.register_type::<MouseButtonInput>()
|
|
.register_type::<PinchGesture>()
|
|
.register_type::<RotationGesture>()
|
|
.register_type::<DoubleTapGesture>()
|
|
.register_type::<PanGesture>()
|
|
.register_type::<TouchInput>()
|
|
.register_type::<GamepadEvent>()
|
|
.register_type::<GamepadButtonInput>()
|
|
.register_type::<GamepadSettings>();
|
|
}
|
|
}
|
|
|
|
/// The current "press" state of an element
|
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Reflect)]
|
|
#[reflect(Debug, Hash, PartialEq)]
|
|
#[cfg_attr(
|
|
feature = "serialize",
|
|
derive(serde::Serialize, serde::Deserialize),
|
|
reflect(Serialize, Deserialize)
|
|
)]
|
|
pub enum ButtonState {
|
|
/// The button is pressed.
|
|
Pressed,
|
|
/// The button is not pressed.
|
|
Released,
|
|
}
|
|
|
|
impl ButtonState {
|
|
/// Is this button pressed?
|
|
pub fn is_pressed(&self) -> bool {
|
|
matches!(self, ButtonState::Pressed)
|
|
}
|
|
}
|