You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently polling for keyboard and other peripheral input requires several layers of de-structuring to access the relevant Winit WindowEvent of interest. The state of a KeyEvent can only be pressed or released, which requires the user to add checks if they would only like to register the event for one frame until the button is released and pressed again. This behaviour is present in the Platformer tutorial, where vertical velocity is continually applied if you hold the space bar to jump.
I would like to propose a different system to reduce boilerplate code in scripts and add a more user-friendly way to interface with the input events. The desired user-facing experience would look something like methods to check for pressedreleased and also add just_pressed and just_released which are true for only one frame, as well as other abilities to manually change or clear current states. Similar to Bevy with the ButtonInput<T> struct, all current input events are added to hash sets for each type which can be more directly accessed.
This way, instead of:
if let Event::WindowEvent { event, .. } = event {
if let WindowEvent::KeyboardInput { event, .. } = event {
let pressed = event.state == ElementState::Pressed;
match event.physical_key {
PhysicalKey::Code(KeyCode::KeyA) => self.move_left = pressed,
PhysicalKey::Code(KeyCode::KeyD) => self.move_right = pressed,
PhysicalKey::Code(KeyCode::KeyW) => self.move_up = pressed,
PhysicalKey::Code(KeyCode::KeyS) => self.move_down = pressed,
_ => {}
}
}
}
One might be able to simply modify struct members directly:
if keys.any_pressed([KeyCode::ArrowUp, KeyCode::KeyW]) {
new_direction.y += 1.0;
}
if keys.any_pressed([KeyCode::ArrowDown, KeyCode::KeyS]) {
new_direction.y -= 1.0;
}
if keys.any_pressed([KeyCode::ArrowRight, KeyCode::KeyD]) {
new_direction.x += 1.0;
}
if keys.any_pressed([KeyCode::ArrowLeft, KeyCode::KeyA]) {
new_direction.x -= 1.0;
}
This makes it very easy to create an input map that would be user-configurable and iterate over to poll for abilities and action inputs with fewer conditional checks for button state and less boilerplate de-structuring.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Currently polling for keyboard and other peripheral input requires several layers of de-structuring to access the relevant Winit
WindowEvent
of interest. The state of aKeyEvent
can only be pressed or released, which requires the user to add checks if they would only like to register the event for one frame until the button is released and pressed again. This behaviour is present in the Platformer tutorial, where vertical velocity is continually applied if you hold the space bar to jump.I would like to propose a different system to reduce boilerplate code in scripts and add a more user-friendly way to interface with the input events. The desired user-facing experience would look something like methods to check for
pressed
released
and also addjust_pressed
andjust_released
which are true for only one frame, as well as other abilities to manually change or clear current states. Similar to Bevy with theButtonInput<T>
struct, all current input events are added to hash sets for each type which can be more directly accessed.This way, instead of:
One might be able to simply modify struct members directly:
This makes it very easy to create an input map that would be user-configurable and iterate over to poll for abilities and action inputs with fewer conditional checks for button state and less boilerplate de-structuring.
Beta Was this translation helpful? Give feedback.
All reactions