Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/input/gamepad_input_events.rs
6595 views
1
//! Iterates and prints gamepad input and connection events.
2
3
use bevy::{
4
input::gamepad::{
5
GamepadAxisChangedEvent, GamepadButtonChangedEvent, GamepadButtonStateChangedEvent,
6
GamepadConnectionEvent, GamepadEvent,
7
},
8
prelude::*,
9
};
10
11
fn main() {
12
App::new()
13
.add_plugins(DefaultPlugins)
14
.add_systems(Update, (gamepad_events, gamepad_ordered_events))
15
.run();
16
}
17
18
fn gamepad_events(
19
mut connection_events: EventReader<GamepadConnectionEvent>,
20
// Handles the continuous measure of an axis, equivalent to GamepadAxes::get.
21
mut axis_changed_events: EventReader<GamepadAxisChangedEvent>,
22
// Handles the continuous measure of how far a button has been pressed down, equivalent to `GamepadButtons::get`.
23
mut button_changed_events: EventReader<GamepadButtonChangedEvent>,
24
// Handles the boolean measure of whether a button is considered pressed or unpressed, as
25
// defined by the thresholds in `GamepadSettings::button_settings`.
26
// When the threshold is crossed and the button state changes, this event is emitted.
27
mut button_input_events: EventReader<GamepadButtonStateChangedEvent>,
28
) {
29
for connection_event in connection_events.read() {
30
info!("{:?}", connection_event);
31
}
32
for axis_changed_event in axis_changed_events.read() {
33
info!(
34
"{:?} of {} is changed to {}",
35
axis_changed_event.axis, axis_changed_event.entity, axis_changed_event.value
36
);
37
}
38
for button_changed_event in button_changed_events.read() {
39
info!(
40
"{:?} of {} is changed to {}",
41
button_changed_event.button, button_changed_event.entity, button_changed_event.value
42
);
43
}
44
for button_input_event in button_input_events.read() {
45
info!("{:?}", button_input_event);
46
}
47
}
48
49
// If you require in-frame relative event ordering, you can also read the `Gamepad` event
50
// stream directly. For standard use-cases, reading the events individually or using the
51
// `Input<T>` or `Axis<T>` resources is preferable.
52
fn gamepad_ordered_events(mut gamepad_events: EventReader<GamepadEvent>) {
53
for gamepad_event in gamepad_events.read() {
54
match gamepad_event {
55
GamepadEvent::Connection(connection_event) => info!("{:?}", connection_event),
56
GamepadEvent::Button(button_event) => info!("{:?}", button_event),
57
GamepadEvent::Axis(axis_event) => info!("{:?}", axis_event),
58
}
59
}
60
}
61
62