Skip to content

Commit

Permalink
Rename KeyCode to PhysicalKey
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrixyz committed Feb 4, 2024
1 parent 71be08a commit b50c1eb
Show file tree
Hide file tree
Showing 63 changed files with 721 additions and 712 deletions.
22 changes: 11 additions & 11 deletions crates/bevy_input/src/common_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use std::hash::Hash;
/// ```no_run
/// # use bevy_app::{App, NoopPluginGroup as DefaultPlugins, Update};
/// # use bevy_ecs::prelude::IntoSystemConfigs;
/// # use bevy_input::{common_conditions::input_toggle_active, prelude::KeyCode};
/// # use bevy_input::{common_conditions::input_toggle_active, prelude::PhysicalKey};
///
/// fn main() {
/// App::new()
/// .add_plugins(DefaultPlugins)
/// .add_systems(Update, pause_menu.run_if(input_toggle_active(false, KeyCode::Escape)))
/// .add_systems(Update, pause_menu.run_if(input_toggle_active(false, PhysicalKey::Escape)))
/// .run();
/// }
///
Expand All @@ -26,7 +26,7 @@ use std::hash::Hash;
/// ```no_run
/// # use bevy_app::{App, NoopPluginGroup as DefaultPlugins, Update};
/// # use bevy_ecs::prelude::{IntoSystemConfigs, Res, ResMut, Resource};
/// # use bevy_input::{common_conditions::input_just_pressed, prelude::KeyCode};
/// # use bevy_input::{common_conditions::input_just_pressed, prelude::PhysicalKey};
///
/// #[derive(Resource, Default)]
/// struct Paused(bool);
Expand All @@ -35,7 +35,7 @@ use std::hash::Hash;
/// App::new()
/// .add_plugins(DefaultPlugins)
/// .init_resource::<Paused>()
/// .add_systems(Update, toggle_pause_state.run_if(input_just_pressed(KeyCode::Escape)))
/// .add_systems(Update, toggle_pause_state.run_if(input_just_pressed(PhysicalKey::Escape)))
/// .add_systems(Update, pause_menu.run_if(|paused: Res<Paused>| paused.0))
/// .run();
/// }
Expand Down Expand Up @@ -76,11 +76,11 @@ where
/// ```no_run
/// # use bevy_app::{App, NoopPluginGroup as DefaultPlugins, Update};
/// # use bevy_ecs::prelude::IntoSystemConfigs;
/// # use bevy_input::{common_conditions::input_just_pressed, prelude::KeyCode};
/// # use bevy_input::{common_conditions::input_just_pressed, prelude::PhysicalKey};
/// fn main() {
/// App::new()
/// .add_plugins(DefaultPlugins)
/// .add_systems(Update, jump.run_if(input_just_pressed(KeyCode::Space)))
/// .add_systems(Update, jump.run_if(input_just_pressed(PhysicalKey::Space)))
/// .run();
/// }
///
Expand All @@ -104,7 +104,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::prelude::KeyCode;
use crate::prelude::PhysicalKey;
use bevy_ecs::schedule::{IntoSystemConfigs, Schedule};

fn test_system() {}
Expand All @@ -114,10 +114,10 @@ mod tests {
fn distributive_run_if_compiles() {
Schedule::default().add_systems(
(test_system, test_system)
.distributive_run_if(input_toggle_active(false, KeyCode::Escape))
.distributive_run_if(input_pressed(KeyCode::Escape))
.distributive_run_if(input_just_pressed(KeyCode::Escape))
.distributive_run_if(input_just_released(KeyCode::Escape)),
.distributive_run_if(input_toggle_active(false, PhysicalKey::Escape))
.distributive_run_if(input_pressed(PhysicalKey::Escape))
.distributive_run_if(input_just_pressed(PhysicalKey::Escape))
.distributive_run_if(input_just_released(PhysicalKey::Escape)),
);
}
}
26 changes: 13 additions & 13 deletions crates/bevy_input/src/keyboard.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
//! The keyboard input functionality.

// This file contains a substantial portion of the UI Events Specification by the W3C. In
// particular, the variant names within `KeyCode` and their documentation are modified
// particular, the variant names within `PhysicalKey` and their documentation are modified
// versions of contents of the aforementioned specification.
//
// The original documents are:
//
//
// ### For `KeyCode`
// ### For `PhysicalKey`
// UI Events KeyboardEvent code Values
// https://www.w3.org/TR/2017/CR-uievents-code-20170601/
// Copyright © 2017 W3C® (MIT, ERCIM, Keio, Beihang).
//
// These documents were used under the terms of the following license. This W3C license as well as
// the W3C short notice apply to the `KeyCode` enums and their variants and the
// the W3C short notice apply to the `PhysicalKey` enums and their variants and the
// documentation attached to their variants.

// --------- BEGGINING OF W3C LICENSE --------------------------------------------------------------
Expand Down Expand Up @@ -86,7 +86,7 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
/// ## Usage
///
/// The event is consumed inside of the [`keyboard_input_system`]
/// to update the [`Input<KeyCode>`](ButtonInput<KeyCode>) resource.
/// to update the [`Input<PhysicalKey>`](ButtonInput<PhysicalKey>) resource.
#[derive(Event, Debug, Clone, PartialEq, Eq, Reflect)]
#[reflect(Debug, PartialEq)]
#[cfg_attr(
Expand All @@ -96,7 +96,7 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
)]
pub struct KeyboardInput {
/// The physical key code of the key.
pub key_code: KeyCode,
pub key_code: PhysicalKey,
/// The logical key of the input
pub logical_key: Key,
/// The press state of the key.
Expand All @@ -105,14 +105,14 @@ pub struct KeyboardInput {
pub window: Entity,
}

/// Updates the [`ButtonInput<KeyCode>`] resource with the latest [`KeyboardInput`] events.
/// Updates the [`ButtonInput<PhysicalKey>`] resource with the latest [`KeyboardInput`] events.
///
/// ## Differences
///
/// The main difference between the [`KeyboardInput`] event and the [`ButtonInput<KeyCode>`] resources is that
/// The main difference between the [`KeyboardInput`] event and the [`ButtonInput<PhysicalKey>`] resources is that
/// the latter have convenient functions such as [`ButtonInput::pressed`], [`ButtonInput::just_pressed`] and [`ButtonInput::just_released`].
pub fn keyboard_input_system(
mut key_input: ResMut<ButtonInput<KeyCode>>,
mut key_input: ResMut<ButtonInput<PhysicalKey>>,
mut keyboard_input_events: EventReader<KeyboardInput>,
) {
// Avoid clearing if it's not empty to ensure change detection is not triggered.
Expand All @@ -134,8 +134,8 @@ pub fn keyboard_input_system(
/// enum), but the values are primarily tied to the key's physical location on the keyboard.
///
/// This enum is primarily used to store raw keycodes when Winit doesn't map a given native
/// physical key identifier to a meaningful [`KeyCode`] variant. In the presence of identifiers we
/// haven't mapped for you yet, this lets you use use [`KeyCode`] to:
/// physical key identifier to a meaningful [`PhysicalKey`] variant. In the presence of identifiers we
/// haven't mapped for you yet, this lets you use use [`PhysicalKey`] to:
///
/// - Correctly match key press and release events.
/// - On non-web platforms, support assigning keybinds to virtually any key through a UI.
Expand All @@ -162,7 +162,7 @@ pub enum NativeKeyCode {
///
/// ## Usage
///
/// It is used as the generic `T` value of an [`ButtonInput`] to create a `Res<Input<KeyCode>>`.
/// It is used as the generic `T` value of an [`ButtonInput`] to create a `Res<ButtonInput<PhysicalKey>>`.
///
/// Code representing the location of a physical key
/// This mostly conforms to the UI Events Specification's [`KeyboardEvent.code`] with a few
Expand All @@ -184,11 +184,11 @@ pub enum NativeKeyCode {
reflect(Serialize, Deserialize)
)]
#[repr(u32)]
pub enum KeyCode {
pub enum PhysicalKey {
/// This variant is used when the key cannot be translated to any other variant.
///
/// The native keycode is provided (if available) so you're able to more reliably match
/// key-press and key-release events by hashing the [`KeyCode`]. It is also possible to use
/// key-press and key-release events by hashing the [`PhysicalKey`]. It is also possible to use
/// this for keybinds for non-standard keys, but such keybinds are tied to a given platform.
Unidentified(NativeKeyCode),
/// <kbd>`</kbd> on a US keyboard. This is also called a backtick or grave.
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_input/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub mod prelude {
gamepad::{
Gamepad, GamepadAxis, GamepadAxisType, GamepadButton, GamepadButtonType, Gamepads,
},
keyboard::KeyCode,
keyboard::PhysicalKey,
mouse::MouseButton,
touch::{TouchInput, Touches},
Axis, ButtonInput,
Expand All @@ -34,7 +34,7 @@ pub mod prelude {
use bevy_app::prelude::*;
use bevy_ecs::prelude::*;
use bevy_reflect::Reflect;
use keyboard::{keyboard_input_system, Key, KeyCode, KeyboardInput, NativeKey, NativeKeyCode};
use keyboard::{keyboard_input_system, Key, KeyboardInput, NativeKey, NativeKeyCode, PhysicalKey};
use mouse::{
mouse_button_input_system, MouseButton, MouseButtonInput, MouseMotion, MouseScrollUnit,
MouseWheel,
Expand Down Expand Up @@ -66,7 +66,7 @@ impl Plugin for InputPlugin {
app
// keyboard
.add_event::<KeyboardInput>()
.init_resource::<ButtonInput<KeyCode>>()
.init_resource::<ButtonInput<PhysicalKey>>()
.add_systems(PreUpdate, keyboard_input_system.in_set(InputSystem))
// mouse
.add_event::<MouseButtonInput>()
Expand Down Expand Up @@ -112,7 +112,7 @@ impl Plugin for InputPlugin {

// Register keyboard types
app.register_type::<KeyboardInput>()
.register_type::<KeyCode>()
.register_type::<PhysicalKey>()
.register_type::<NativeKeyCode>()
.register_type::<Key>()
.register_type::<NativeKey>();
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_window/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{PrimaryWindow, Window, WindowCloseRequested};

use bevy_app::AppExit;
use bevy_ecs::prelude::*;
use bevy_input::{keyboard::KeyCode, ButtonInput};
use bevy_input::{keyboard::PhysicalKey, ButtonInput};

/// Exit the application when there are no open windows.
///
Expand Down Expand Up @@ -52,14 +52,14 @@ pub fn close_when_requested(mut commands: Commands, mut closed: EventReader<Wind
pub fn close_on_esc(
mut commands: Commands,
focused_windows: Query<(Entity, &Window)>,
input: Res<ButtonInput<KeyCode>>,
input: Res<ButtonInput<PhysicalKey>>,
) {
for (window, focus) in focused_windows.iter() {
if !focus.focused {
continue;
}

if input.just_pressed(KeyCode::Escape) {
if input.just_pressed(PhysicalKey::Escape) {
commands.entity(window).despawn();
}
}
Expand Down
Loading

0 comments on commit b50c1eb

Please sign in to comment.