pub trait MockInput {
fn send_input(&mut self, input: impl Into<UserInput>);
fn send_input_as_gamepad(
&mut self,
input: impl Into<UserInput>,
gamepad: Option<Gamepad>
);
fn release_input(&mut self, input: impl Into<UserInput>);
fn release_input_as_gamepad(
&mut self,
input: impl Into<UserInput>,
gamepad: Option<Gamepad>
);
fn pressed(&self, input: impl Into<UserInput>) -> bool;
fn pressed_for_gamepad(
&self,
input: impl Into<UserInput>,
gamepad: Option<Gamepad>
) -> bool;
fn reset_inputs(&mut self);
fn click_button<Marker: Component>(&mut self);
fn hover_button<Marker: Component>(&mut self);
}
Expand description
Send fake input events for testing purposes
In game code, you should (almost) always be setting the ActionState
directly instead.
Examples
use bevy::prelude::*;
use bevy::input::InputPlugin;
use leafwing_input_manager::input_mocking::MockInput;
// Remember to add InputPlugin so the resources will be there!
let mut app = App::new();
app.add_plugin(InputPlugin);
// Pay respects!
app.send_input(KeyCode::F);
app.update();
use bevy::prelude::*;
use bevy::input::InputPlugin;
use leafwing_input_manager::{input_mocking::MockInput, user_input::UserInput};
let mut app = App::new();
app.add_plugin(InputPlugin);
// Send inputs one at a time
let B_E_V_Y = [KeyCode::B, KeyCode::E, KeyCode::V, KeyCode::Y];
for letter in B_E_V_Y {
app.send_input(letter);
}
// Or use chords!
app.send_input(UserInput::chord(B_E_V_Y));
app.update();
Required Methods
fn send_input(&mut self, input: impl Into<UserInput>)
fn send_input(&mut self, input: impl Into<UserInput>)
Send the specified user_input
directly
These are sent as the raw input events, and do not set the value of [Input
] or [Axis
] directly.
Note that inputs will continue to be pressed until explicitly released or MockInput::reset_inputs
is called.
To send specific values for axislike inputs, set their value
field.
Gamepad input will be sent by the first registed controller found. If none are found, gamepad input will be silently skipped.
Warning
You must call app.update()
at least once after sending input
with InputPlugin
included in your plugin set
for the raw input events to be processed into [Input
] and [Axis
] data.
fn send_input_as_gamepad(
&mut self,
input: impl Into<UserInput>,
gamepad: Option<Gamepad>
)
fn send_input_as_gamepad(
&mut self,
input: impl Into<UserInput>,
gamepad: Option<Gamepad>
)
Send the specified user_input
directly, using the specified gamepad
Note that inputs will continue to be pressed until explicitly released or MockInput::reset_inputs
is called.
Provide the [Gamepad
] identifier to control which gamepad you are emulating.
fn release_input(&mut self, input: impl Into<UserInput>)
fn release_input(&mut self, input: impl Into<UserInput>)
Releases the specified user_input
directly
Gamepad input will be released by the first registed controller found. If none are found, gamepad input will be silently skipped.
fn release_input_as_gamepad(
&mut self,
input: impl Into<UserInput>,
gamepad: Option<Gamepad>
)
fn release_input_as_gamepad(
&mut self,
input: impl Into<UserInput>,
gamepad: Option<Gamepad>
)
Releases the specified user_input
directly, using the specified gamepad
Provide the [Gamepad
] identifier to control which gamepad you are emulating.
Is the provided user_input
pressed?
This method is intended as a convenience for testing; check the [Input
] resource directly,
or use an InputMap
in real code.
Is the provided user_input
pressed for the provided [Gamepad
]?
This method is intended as a convenience for testing; check the [Input
] resource directly,
or use an InputMap
in real code.
fn reset_inputs(&mut self)
fn reset_inputs(&mut self)
Clears all user input streams, resetting them to their default state
All buttons are released, and just_pressed
and just_released
information on the [Input
] type are lost.
just_pressed
and just_released
on the ActionState
will be kept.
This will clear all [KeyCode
], [GamepadButton
] and [MouseButton
] input streams,
as well as any [Interaction
] components and all input [Events
].
Presses all bevy::ui
buttons with the matching Marker
component
Changes their [Interaction
] component to [Interaction::Clicked
]
Hovers over all bevy::ui
buttons with the matching Marker
component
Changes their [Interaction
] component to [Interaction::Clicked
]