bevy_input_bindings 0.0.2

High level, flexible and shareable input binding library for the Bevy game engine
Documentation
use bevy::prelude::*;

use bevy_input_bindings::prelude::*;

fn main() {
    App::new()
        .add_plugins((
            DefaultPlugins,
            // Add the keyboard plugin attached to the custom binder
            keyboard::Plugin::<MyActions, NoValues, MyKeyboardBinding>::new(),
        ))
        // Add an observer to print the inputs. It will catch events for the `MyActions` actions,
        // find the `ControllerInterface` that sent it, and print the event data.
        .add_observer(
            |action: On<ActionEvent<MyActions>>, controllers: Query<&ControllerInterface>| {
                // Find the controller that sent the event
                let Ok(controller) = controllers.get(action.controller()) else {
                    return;
                };
                // Print data about this action
                println!(
                    "Action from {}({:?}): {:?}",
                    action.controller(),
                    controller.layout(),
                    action.key()
                );
            },
        )
        // Add a startup system to initiate the entities
        .add_systems(Startup, |mut commands: Commands| {
            commands.spawn(Camera3d::default());
            // Here we instantiate two binders with different bindings so two players can play on
            // the same keyboard.
            commands.spawn(MyKeyboardBinding::keyboard1());
            commands.spawn(MyKeyboardBinding::keyboard2());
        })
        .run();
}

/// Custom actions for this example
#[derive(Debug)]
enum MyActions {
    Jump,
    Shoot,
}

/// Custom values for this example
#[derive(Debug, Hash, PartialEq, Eq)]
struct NoValues;

/// Custom binder for this example
#[derive(Component, Reflect)]
struct MyKeyboardBinding {
    jump: keyboard::triggers::ButtonPressed,
    shoot: keyboard::triggers::ButtonPressed,
}

impl MyKeyboardBinding {
    /// Create a keyboard binder on the first part of the keyboard
    fn keyboard1() -> Self {
        Self {
            jump: keyboard::triggers::ButtonPressed::new(KeyCode::KeyE),
            shoot: keyboard::triggers::ButtonPressed::new(KeyCode::KeyR),
        }
    }

    /// Create a keyboard binder on the second part of the keyboard
    fn keyboard2() -> Self {
        Self {
            jump: keyboard::triggers::ButtonPressed::new(KeyCode::KeyU),
            shoot: keyboard::triggers::ButtonPressed::new(KeyCode::KeyY),
        }
    }
}

impl keyboard::Binding<MyActions, NoValues> for MyKeyboardBinding {
    fn get_action_bindings<'a>(
        &'a mut self,
    ) -> Vec<(&'a mut dyn keyboard::TriggerTrait, MyActions)> {
        return vec![(&mut self.jump, MyActions::Jump), (&mut self.shoot, MyActions::Shoot)];
    }
}