Event
RMK uses an event-driven architecture for communication between components. Events are type-safe messages that flow through channels, connecting input devices, processors, and the keyboard core.
Built-in Events
RMK provides built-in event types organized by category:
Input Events (rmk::event::input):
KeyboardEvent- Key press/release event from matrix or encodersModifierEvent- Modifier key combination changesPointingEvent- Pointing device events (mouse movement, scroll)
State Events (rmk::event::state):
LayerChangeEvent- Active layer changedLedIndicatorEvent- LED indicator state changed (NumLock, CapsLock, ScrollLock)WpmUpdateEvent- Words per minute updatedSleepStateEvent- Sleep state changed
Battery Events (rmk::event::battery):
BatteryAdcEvent- Raw battery ADC readingChargingStateEvent- Charging state changedBatteryStateEvent- Battery state changed (includes level and charging status)
Connection Events (rmk::event::connection):
ConnectionChangeEvent- Connection type changed (USB/BLE)BleStatusChangeEvent- BLE profile or connection state changed (when BLE is enabled)
Split Keyboard Events (rmk::event::split, when split is enabled):
PeripheralConnectedEvent- Peripheral connection state changedCentralConnectedEvent- Connected to central state changedPeripheralBatteryEvent- Peripheral battery state changedClearPeerEvent- BLE peer clearing event
Defining Custom Events
Use the #[event] macro to define custom events:
Parameters:
channel_size(optional): Buffer size of the event channel. Default is 8 for Channel, 1 for PubSub.subs(optional): Max subscribers. If specified, uses PubSub channel. Default is 4.pubs(optional): Max publishers. If specified, uses PubSub channel. Default is 1.
::: note Channel types
- Channel: Each event is consumed by one subscriber. If multiple subscribers exist, only one receives each event.
- PubSub: Each event is broadcast to all subscribers. Specify
subsorpubsto enable. :::
Multi-event Enums
When a component produces multiple types of events, use #[derive(Event)] on an enum:
When published, each variant is automatically routed to its underlying event channel.
Publishing Events
Events are published using publish_event or publish_event_async:
Related Documentation
- Input Device - How to create input devices that publish events
- Processor - How to create processors that subscribe to events
- Event Configuration - How to configure event channels in keyboard.toml