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 two plugins
            keyboard::Plugin::<FirstActions, NoValues, FirstKeyboardBinding>::new(),
            keyboard::Plugin::<SecondActions, NoValues, SecondKeyboardBinding>::new(),
        ))
        // Add an observer to print the inputs of the first binder.
        .add_observer(|action: On<ActionEvent<FirstActions>>| {
            // Print data about this action
            println!("First binder action from {}: {:?}", action.controller(), action.key());
        })
        // Add an observer to print the inputs of the second binder.
        .add_observer(|action: On<ActionEvent<SecondActions>>| {
            // Print data about this action
            println!("Second binder action from {}: {:?}", action.controller(), action.key());
        })
        // Add a startup system to initiate the entities
        .add_systems(Startup, |mut commands: Commands| {
            commands.spawn(Camera3d::default());
            // The keyboard bindings needs to be manually instantiated.
            commands.spawn((FirstKeyboardBinding::new(), SecondKeyboardBinding::new()));
        })
        .run();
}

#[derive(Debug)]
enum FirstActions {
    Jump,
}

#[derive(Debug, Hash, PartialEq, Eq)]
struct NoValues;

#[derive(Component, Reflect)]
struct FirstKeyboardBinding {
    jump: keyboard::triggers::ButtonPressed,
}

impl FirstKeyboardBinding {
    fn new() -> Self {
        Self {
            jump: keyboard::triggers::ButtonPressed::new(KeyCode::KeyQ),
        }
    }
}

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

#[derive(Debug)]
enum SecondActions {
    Shoot,
}

#[derive(Component, Reflect)]
struct SecondKeyboardBinding {
    shoot: keyboard::triggers::ButtonPressed,
}

impl SecondKeyboardBinding {
    fn new() -> Self {
        Self {
            shoot: keyboard::triggers::ButtonPressed::new(KeyCode::KeyW),
        }
    }
}

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