Skip to main content

rmk_types/keycode/
system_control.rs

1//! System control keycodes.
2
3use postcard::experimental::max_size::MaxSize;
4use serde::{Deserialize, Serialize};
5
6use super::hid::HidKeyCode;
7
8/// Keys in `Generic Desktop Page`, generally used for system control
9/// Ref: <https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf#page=26>
10#[non_exhaustive]
11#[repr(u8)]
12#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, PartialOrd, Ord, MaxSize)]
13#[cfg_attr(feature = "_codegen", derive(strum::EnumIter))]
14#[cfg_attr(feature = "defmt", derive(defmt::Format))]
15#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
16#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
17pub enum SystemControlKey {
18    No = 0x00,
19    PowerDown = 0x81,
20    Sleep = 0x82,
21    WakeUp = 0x83,
22    Restart = 0x8F,
23}
24
25impl SystemControlKey {
26    /// Host-only: list all system-control keys, in declaration order.
27    #[cfg(feature = "_codegen")]
28    pub fn all() -> impl Iterator<Item = Self> {
29        <Self as strum::IntoEnumIterator>::iter()
30    }
31
32    /// Convert SystemControlKey to the corresponding HidKeyCode
33    pub fn to_hid_keycode(&self) -> Option<HidKeyCode> {
34        match self {
35            SystemControlKey::PowerDown => Some(HidKeyCode::SystemPower),
36            SystemControlKey::Sleep => Some(HidKeyCode::SystemSleep),
37            SystemControlKey::WakeUp => Some(HidKeyCode::SystemWake),
38            _ => None,
39        }
40    }
41}