Skip to main content

rmk_types/action/
mod.rs

1//! Keyboard actions and behaviors.
2//!
3//! This module defines the core action system used in RMK firmware.
4//! Actions represent what happens when a key is pressed, from simple key
5//! presses to complex behaviors like tap-hold, layer switching, and macros.
6//!
7//! Key types:
8//! - [`Action`] - Single basic operations (key press, layer switch, macro trigger, etc.)
9//! - [`KeyAction`] - Composite behaviors stored in the keymap (tap-hold, morse, etc.)
10//! - [`EncoderAction`] - Rotary encoder actions
11//! - [`LightAction`] - Light control actions
12//! - [`KeyboardAction`] - Keyboard control actions (reboot, toggle features, etc.)
13//! - [`crate::morse::MorseProfile`] / [`crate::morse::MorseMode`] - Morse/tap-hold timing configuration
14
15mod encoder;
16mod key_action;
17mod keyboard;
18mod light;
19
20pub use encoder::EncoderAction;
21pub use key_action::KeyAction;
22pub use keyboard::KeyboardAction;
23pub use light::LightAction;
24use postcard::experimental::max_size::MaxSize;
25use serde::{Deserialize, Serialize};
26
27use crate::keycode::{HidKeyCode, KeyCode, SpecialKey};
28use crate::modifier::ModifierCombination;
29#[cfg(feature = "steno")]
30use crate::steno::StenoKey;
31
32/// A single basic action that a keyboard can execute.
33#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, MaxSize)]
34#[cfg_attr(feature = "defmt", derive(defmt::Format))]
35#[non_exhaustive]
36#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
37#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
38pub enum Action {
39    /// Default action, no action.
40    No,
41    /// A normal key stroke, uses for all keycodes defined in `KeyCode` enum, including mouse key, consumer/system control, etc.
42    Key(KeyCode),
43    /// Modifier Combination, used in tap hold
44    Modifier(ModifierCombination),
45    /// Key stroke with modifier combination triggered. Modifiers only apply to
46    /// HID keyboard keys, so this carries a [`HidKeyCode`] rather than a full [`KeyCode`].
47    KeyWithModifier(HidKeyCode, ModifierCombination),
48    /// Activate a layer
49    LayerOn(u8),
50    /// Activate a layer with modifier combination triggered.
51    LayerOnWithModifier(u8, ModifierCombination),
52    /// Deactivate a layer
53    LayerOff(u8),
54    /// Toggle a layer
55    LayerToggle(u8),
56    /// Set default layer
57    DefaultLayer(u8),
58    /// Activate a layer and deactivate all other layers(except default layer)
59    LayerToggleOnly(u8),
60    TriLayerLower,
61    TriLayerUpper,
62    /// Triggers the Macro at the 'index'.
63    TriggerMacro(u8),
64    /// Oneshot layer, keep the layer active until the next key is triggered.
65    OneShotLayer(u8),
66    /// Oneshot modifier, keep the modifier active until the next key is triggered.
67    OneShotModifier(ModifierCombination),
68    /// Oneshot key, keep the key active until the next key is triggered.
69    OneShotKey(HidKeyCode),
70    /// Actions for controlling lights
71    Light(LightAction),
72    /// Actions for controlling the keyboard
73    KeyboardControl(KeyboardAction),
74    /// Special Keys
75    Special(SpecialKey),
76    /// User Keys
77    User(u8),
78    /// Set the default layer and persist it to storage; restored on next boot.
79    ///
80    /// Runtime behavior matches [`Action::DefaultLayer`]; additionally persisted to flash.
81    PersistentDefaultLayer(u8),
82    /// A Plover HID stenography key. Press/release of this key updates the
83    /// in-progress steno chord; on first release the accumulated chord is
84    /// sent to the host as a vendor HID report.
85    #[cfg(feature = "steno")]
86    Steno(StenoKey),
87}