| Crates.io | bevy_keyboard_shortcuts |
| lib.rs | bevy_keyboard_shortcuts |
| version | 0.3.0 |
| created_at | 2025-10-28 11:01:08.340955+00 |
| updated_at | 2026-01-17 14:06:09.081886+00 |
| description | Keyboard shortcut system for Bevy applications |
| homepage | |
| repository | https://github.com/peterellisjones/bevy_keyboard_shortcuts |
| max_upload_size | |
| id | 1904560 |
| size | 146,230 |
A Bevy plugin for handling keyboard shortcuts with modifier support.
This crate provides a flexible way to define and check keyboard shortcuts in Bevy applications, with support for modifier keys (Ctrl, Alt, Shift, Super) and both single-press and repeating inputs.
use bevy::prelude::*;
use bevy_keyboard_shortcuts::Shortcuts;
fn check_shortcuts(keyboard: Res<ButtonInput<KeyCode>>) {
let jump = Shortcuts::single_press(&[KeyCode::Space]);
let save = Shortcuts::single_press(&[KeyCode::KeyS]).with_ctrl();
if jump.pressed(&keyboard) {
println!("Jump!");
}
if save.pressed(&keyboard) {
println!("Saving!");
}
}
A typical usage pattern involves creating a Bevy Resource with your shortcuts:
use bevy::prelude::*;
use bevy_keyboard_shortcuts::Shortcuts;
use serde::{Deserialize, Serialize};
#[derive(Resource)]
pub struct ShortcutSettings {
pub move_left: Shortcuts,
pub move_right: Shortcuts,
pub quick_save: Shortcuts,
}
impl Default for ShortcutSettings {
fn default() -> Self {
Self {
move_left: Shortcuts::repeating(&[KeyCode::KeyA, KeyCode::ArrowLeft]),
move_right: Shortcuts::repeating(&[KeyCode::KeyD, KeyCode::ArrowRight]),
quick_save: Shortcuts::single_press(&[KeyCode::KeyS]).with_ctrl(),
}
}
}
fn handle_input(
keyboard: Res<ButtonInput<KeyCode>>,
shortcuts: Res<ShortcutSettings>,
) {
if shortcuts.move_left.pressed(&keyboard) {
// Move left
}
if shortcuts.move_right.pressed(&keyboard) {
// Move right
}
if shortcuts.quick_save.pressed(&keyboard) {
// Save game
}
}
This crate works well with configuration files. Here's an example YAML configuration:
# Camera controls - repeating shortcuts (continuous input)
move_left:
repeats: true
shortcuts:
- key: "KeyA"
- key: "ArrowLeft"
move_right:
repeats: true
shortcuts:
- key: "KeyD"
- key: "ArrowRight"
# Example with modifiers
save:
repeats: false
shortcuts:
- key: "KeyS"
modifiers:
control: RequirePressed
Key names in YAML/JSON configuration match Bevy's KeyCode enum variants exactly. Remove the KeyCode:: prefix:
KeyA, KeyB, ..., KeyZDigit0, Digit1, ..., Digit9F1, F2, ..., F12ArrowUp, ArrowDown, ArrowLeft, ArrowRightSpace, Enter, Escape, Tab, Backspace, DeleteNumpad0, Numpad1, ..., Numpad9, NumpadAdd, NumpadSubtractFor example: KeyCode::KeyA becomes "KeyA", KeyCode::Space becomes "Space".
For the complete list, see Bevy's KeyCode documentation.
By default, all modifiers are ignored - shortcuts trigger regardless of modifier state.
Each modifier key (Ctrl, Alt, Shift, Super) can be configured:
.with_ctrl(), .with_alt(), .with_shift(), .with_super()control: RequirePressed, alt: RequirePressed, etc..without_ctrl(), .without_alt(), .without_shift(), .without_super()control: RequireNotPressed, alt: RequireNotPressed, etc.// Default - ignores all modifiers
let flexible = Shortcuts::single_press(&[KeyCode::KeyA]);
// Requires Ctrl
let save = Shortcuts::single_press(&[KeyCode::KeyS]).with_ctrl();
// Requires Ctrl AND Shift
let redo = Shortcuts::single_press(&[KeyCode::KeyZ]).with_ctrl().with_shift();
// Forbids Ctrl (S without Ctrl)
let action = Shortcuts::single_press(&[KeyCode::KeyS]).without_ctrl();