Skip to content

Commit c3a72e9

Browse files
committed
Add keyboard modifier example (#1656) (#1657)
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 <guim.caballero@gmail.com>
1 parent 48ee167 commit c3a72e9

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ path = "examples/input/gamepad_input_events.rs"
302302
name = "keyboard_input"
303303
path = "examples/input/keyboard_input.rs"
304304

305+
[[example]]
306+
name = "keyboard_modifiers"
307+
path = "examples/input/keyboard_modifiers.rs"
308+
305309
[[example]]
306310
name = "keyboard_input_events"
307311
path = "examples/input/keyboard_input_events.rs"

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ Example | File | Description
163163
`gamepad_input_events` | [`input/gamepad_input_events.rs`](./input/gamepad_input_events.rs) | Iterates and prints gamepad input and connection events
164164
`keyboard_input` | [`input/keyboard_input.rs`](./input/keyboard_input.rs) | Demonstrates handling a key press/release
165165
`keyboard_input_events` | [`input/keyboard_input_events.rs`](./input/keyboard_input_events.rs) | Prints out all keyboard events
166+
`keyboard_modifiers` | [`input/keyboard_modifiers.rs`](./input/keyboard_modifiers.rs) | Demonstrates using key modifiers (ctrl, shift)
166167
`mouse_input` | [`input/mouse_input.rs`](./input/mouse_input.rs) | Demonstrates handling a mouse button press/release
167168
`mouse_input_events` | [`input/mouse_input_events.rs`](./input/mouse_input_events.rs) | Prints out all mouse events (buttons, movement, etc.)
168169
`touch_input` | [`input/touch_input.rs`](./input/touch_input.rs) | Displays touch presses, releases, and cancels
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use bevy::{
2+
input::{keyboard::KeyCode, Input},
3+
prelude::*,
4+
};
5+
6+
fn main() {
7+
App::build()
8+
.add_plugins(DefaultPlugins)
9+
.add_system(keyboard_input_system.system())
10+
.run();
11+
}
12+
13+
/// This system prints when Ctrl + Shift + A is pressed
14+
fn keyboard_input_system(input: Res<Input<KeyCode>>) {
15+
let shift = input.pressed(KeyCode::LShift) || input.pressed(KeyCode::RShift);
16+
let ctrl = input.pressed(KeyCode::LControl) || input.pressed(KeyCode::RControl);
17+
18+
if ctrl && shift && input.just_pressed(KeyCode::A) {
19+
println!("Just pressed Ctrl + Shift + A!");
20+
}
21+
}

0 commit comments

Comments
 (0)