Skip to content

Commit

Permalink
Add keyboard modifier example (#1656) (#1657)
Browse files Browse the repository at this point in the history
This PR adds a small example that shows how to use Keyboard modifiers, as shown in [this](#1654 (comment)) snippet.

Fixes #1656.

Co-authored-by: guimcaballero <[email protected]>
  • Loading branch information
guimcaballero and guimcaballero committed Mar 14, 2021
1 parent 48ee167 commit c3a72e9
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 c3a72e9

Please sign in to comment.