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
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/bevy_input/Cargo.toml
Expand Up @@ -20,4 +20,4 @@ bevy_math = { path = "../bevy_math", version = "0.8.0-dev" }
bevy_utils = { path = "../bevy_utils", version = "0.8.0-dev" }

# other
serde = { version = "1", features = ["derive"], optional = true }
serde = { version = "1", features = ["derive"], optional = true }
Bleb1k marked this conversation as resolved.
Show resolved Hide resolved
21 changes: 17 additions & 4 deletions crates/bevy_input/src/keyboard.rs
Expand Up @@ -27,10 +27,12 @@ pub struct KeyboardInput {
/// 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`].
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),
Expand All @@ -39,10 +41,17 @@ pub fn keyboard_input_system(
} = event
{
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),
}
}
let KeyboardInput {
scan_code, state, ..
} = event;
match state {
ButtonState::Pressed => scan_input.press(ScanCode(*scan_code)),
ButtonState::Released => scan_input.release(ScanCode(*scan_code)),
}
}
}

Expand Down Expand Up @@ -407,3 +416,7 @@ pub enum KeyCode {
/// The `Cut` key.
Cut,
}

#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct ScanCode(u32);
Bleb1k marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 3 additions & 2 deletions crates/bevy_input/src/lib.rs
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