Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Added keyboard scan input event #5495

Closed
wants to merge 12 commits into from
Closed
44 changes: 32 additions & 12 deletions crates/bevy_input/src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,29 @@ pub struct KeyboardInput {
///
/// ## Differences
///
/// The main difference between the [`KeyboardInput`] event and the [`Input<KeyCode>`] resource is that
/// the latter has convenient functions like [`Input::pressed`], [`Input::just_pressed`] and [`Input::just_released`].
/// The main difference between the [`KeyboardInput`] event and the [`Input<KeyCode>`] or [`Input<ScanCode>`] resources is that
/// the latter have convenient functions such as [`Input::pressed`], [`Input::just_pressed`] and [`Input::just_released`].
pub fn keyboard_input_system(
mut keyboard_input: ResMut<Input<KeyCode>>,
mut scan_input: ResMut<Input<ScanCode>>,
mut key_input: ResMut<Input<KeyCode>>,
mut keyboard_input_events: EventReader<KeyboardInput>,
) {
keyboard_input.clear();
scan_input.clear();
key_input.clear();
for event in keyboard_input_events.iter() {
if let KeyboardInput {
key_code: Some(key_code),
state,
..
} = event
{
let KeyboardInput {
scan_code, state, ..
} = event;
if let Some(key_code) = event.key_code {
match state {
ButtonState::Pressed => keyboard_input.press(*key_code),
ButtonState::Released => keyboard_input.release(*key_code),
ButtonState::Pressed => key_input.press(key_code),
ButtonState::Released => key_input.release(key_code),
}
}
match state {
ButtonState::Pressed => scan_input.press(ScanCode(*scan_code)),
ButtonState::Released => scan_input.release(ScanCode(*scan_code)),
}
}
}

Expand All @@ -52,6 +56,7 @@ pub fn keyboard_input_system(
///
/// It is used as the generic `T` value of an [`Input`](crate::Input) to create a `Res<Input<KeyCode>>`.
/// The resource stores the data of the buttons of a keyboard and can be accessed inside of a system.
/// Use [`KeyCode`](ScanCode) for keyboard-layout independent controls
///
/// ## Updating
///
Expand Down Expand Up @@ -407,3 +412,18 @@ pub enum KeyCode {
/// The `Cut` key.
Cut,
}

/// The scan code of a [`KeyboardInput`](crate::keyboard::KeyboardInput).
///
/// ## Usage
///
/// It is used as the generic `T` value of an [`Input`](crate::Input) to create a `Res<Input<ScanCode>>`.
/// The resource stores the numeration of the buttons of a keyboard and can be accessed inside of a system.
/// Use [`KeyCode`](KeyCode) for more meaningful/readable code
Copy link
Member

@alice-i-cecile alice-i-cecile Jul 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this captures the distinction quite correctly. ScanCodes are used for semantic values, KeyCodes correspond to the position on a keyboard. We should also update KeyCode's docs to link back to here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

@vacuus vacuus Aug 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you mixed up the two

SDL_Scancode values are used to represent the physical location of a keyboard key on the keyboard.
SDL_Keycode values are mapped to the current layout of the keyboard

(from the website you linked)

scancode: ScanCode
Identifies the physical key pressed
virtual_keycode: Option<VirtualKeyCode>
Identifies the semantic meaning of the key

https://docs.rs/winit/latest/winit/event/struct.KeyboardInput.html (winit docs, since Bevy gets KeyboardInput from it)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, thank you.

///
/// ## Updating
///
/// The resource is updated inside of the [`keyboard_input_system`](crate::keyboard::keyboard_input_system).
#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct ScanCode(pub u32);
5 changes: 3 additions & 2 deletions crates/bevy_input/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ pub mod prelude {
Gamepad, GamepadAxis, GamepadAxisType, GamepadButton, GamepadButtonType, GamepadEvent,
GamepadEventType, Gamepads,
},
keyboard::KeyCode,
keyboard::{KeyCode, ScanCode},
mouse::MouseButton,
touch::{TouchInput, Touches},
Axis, Input,
};
}

use bevy_app::prelude::*;
use keyboard::{keyboard_input_system, KeyCode, KeyboardInput};
use keyboard::{keyboard_input_system, KeyCode, KeyboardInput, ScanCode};
use mouse::{mouse_button_input_system, MouseButton, MouseButtonInput, MouseMotion, MouseWheel};
use prelude::Gamepads;
use touch::{touch_screen_input_system, TouchInput, Touches};
Expand All @@ -47,6 +47,7 @@ impl Plugin for InputPlugin {
// keyboard
.add_event::<KeyboardInput>()
.init_resource::<Input<KeyCode>>()
.init_resource::<Input<ScanCode>>()
.add_system_to_stage(
CoreStage::PreUpdate,
keyboard_input_system.label(InputSystem),
Expand Down