Skip to content

Commit

Permalink
Add keyboard modifier example (bevyengine#1656)
Browse files Browse the repository at this point in the history
  • Loading branch information
guimcaballero committed Mar 14, 2021
1 parent de55e05 commit 495ea8a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ path = "examples/input/gamepad_input_events.rs"
name = "keyboard_input"
path = "examples/input/keyboard_input.rs"

[[example]]
name = "keyboard_modifiers"
path = "examples/input/keyboard_modifiers.rs"

[[example]]
name = "keyboard_input_events"
path = "examples/input/keyboard_input_events.rs"
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ Example | File | Description
`gamepad_input_events` | [`input/gamepad_input_events.rs`](./input/gamepad_input_events.rs) | Iterates and prints gamepad input and connection events
`keyboard_input` | [`input/keyboard_input.rs`](./input/keyboard_input.rs) | Demonstrates handling a key press/release
`keyboard_input_events` | [`input/keyboard_input_events.rs`](./input/keyboard_input_events.rs) | Prints out all keyboard events
`keyboard_modifiers` | [`input/keyboard_modifiers.rs`](./input/keyboard_modifiers.rs) | Demonstrates using key modifiers (ctrl, shift)
`mouse_input` | [`input/mouse_input.rs`](./input/mouse_input.rs) | Demonstrates handling a mouse button press/release
`mouse_input_events` | [`input/mouse_input_events.rs`](./input/mouse_input_events.rs) | Prints out all mouse events (buttons, movement, etc.)
`touch_input` | [`input/touch_input.rs`](./input/touch_input.rs) | Displays touch presses, releases, and cancels
Expand Down
21 changes: 21 additions & 0 deletions examples/input/keyboard_modifiers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use bevy::{
input::{keyboard::KeyCode, Input},
prelude::*,
};

fn main() {
App::build()
.add_plugins(DefaultPlugins)
.add_system(keyboard_input_system.system())
.run();
}

/// This system prints when Ctrl + Shift + A is pressed
fn keyboard_input_system(input: Res<Input<KeyCode>>) {
let shift = input.pressed(KeyCode::LShift) || input.pressed(KeyCode::RShift);
let ctrl = input.pressed(KeyCode::LControl) || input.pressed(KeyCode::RControl);

if ctrl && shift && input.just_pressed(KeyCode::A) {
println!("Just pressed Ctrl + Shift + A!");
}
}

0 comments on commit 495ea8a

Please sign in to comment.