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>>, mut binders: Query<&mut MyKeyboardBinding>| {
                // Print data about this action
                println!("Action from {}: {:?}", action.controller(), action.key());
                // Find the controller that sent the event
                let Ok(mut binder) = binders.get_mut(action.controller()) else {
                    return;
                };
                // Switch Jump and Shoot actions
                match action.key() {
                    MyActions::Jump => {
                        binder.jump_or_shoot = false;
                    },
                    MyActions::Shoot => {
                        binder.jump_or_shoot = true;
                    },
                }
            },
        )
        // Add a startup system to initiate the entities
        .add_systems(Startup, |mut commands: Commands| {
            commands.spawn(Camera2d::default());
            // The keyboard bindings needs to be manually instantiated.
            commands.spawn(MyKeyboardBinding::new());
        })
        .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_or_shoot: bool,
    jump: keyboard::triggers::ButtonPressed,
    shoot: keyboard::triggers::ButtonPressed,
}

impl MyKeyboardBinding {
    /// Create a keyboard binder
    fn new() -> Self {
        Self {
            jump_or_shoot: true,
            jump: keyboard::triggers::ButtonPressed::new(KeyCode::KeyQ),
            shoot: keyboard::triggers::ButtonPressed::new(KeyCode::KeyQ),
        }
    }
}

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