From c3a72e9dc85b53a1448138270c72e806fc91ec85 Mon Sep 17 00:00:00 2001 From: Guim Caballero Date: Sun, 14 Mar 2021 21:00:36 +0000 Subject: [PATCH] Add keyboard modifier example (#1656) (#1657) This PR adds a small example that shows how to use Keyboard modifiers, as shown in [this](https://github.com/bevyengine/bevy/issues/1654#issuecomment-798966921) snippet. Fixes #1656. Co-authored-by: guimcaballero --- Cargo.toml | 4 ++++ examples/README.md | 1 + examples/input/keyboard_modifiers.rs | 21 +++++++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 examples/input/keyboard_modifiers.rs diff --git a/Cargo.toml b/Cargo.toml index c1e1f34230..c1a688239d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/examples/README.md b/examples/README.md index 51ac09442d..0fe69bfa5d 100644 --- a/examples/README.md +++ b/examples/README.md @@ -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 diff --git a/examples/input/keyboard_modifiers.rs b/examples/input/keyboard_modifiers.rs new file mode 100644 index 0000000000..8e6fb589e0 --- /dev/null +++ b/examples/input/keyboard_modifiers.rs @@ -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>) { + 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!"); + } +}