Skip to main content

usb_stack/device/class/hid/
keyboard.rs

1//! Boot-compatible keyboard.
2
3use crate::device::bus::UsbBus;
4use crate::device::class::hid::{HidDevice, HidSpec, hid_protocol, hid_subclass};
5
6/// Boot keyboard report descriptor: 8-byte input report, 1-byte LED output report.
7pub const KEYBOARD_REPORT_DESCRIPTOR: &[u8] = &[
8    0x05, 0x01, // Usage Page (Generic Desktop)
9    0x09, 0x06, // Usage (Keyboard)
10    0xA1, 0x01, // Collection (Application)
11    // Modifier keys (8 bits)
12    0x05, 0x07, //   Usage Page (Keyboard/Keypad)
13    0x19, 0xE0, //   Usage Minimum (Left Control)
14    0x29, 0xE7, //   Usage Maximum (Right GUI)
15    0x15, 0x00, //   Logical Minimum (0)
16    0x25, 0x01, //   Logical Maximum (1)
17    0x75, 0x01, //   Report Size (1)
18    0x95, 0x08, //   Report Count (8)
19    0x81, 0x02, //   Input (Data, Variable, Absolute)
20    // Reserved byte
21    0x75, 0x08, //   Report Size (8)
22    0x95, 0x01, //   Report Count (1)
23    0x81, 0x01, //   Input (Constant)
24    // LED output (5 bits + 3 padding)
25    0x05, 0x08, //   Usage Page (LEDs)
26    0x19, 0x01, //   Usage Minimum (Num Lock)
27    0x29, 0x05, //   Usage Maximum (Kana)
28    0x75, 0x01, //   Report Size (1)
29    0x95, 0x05, //   Report Count (5)
30    0x91, 0x02, //   Output (Data, Variable, Absolute)
31    0x75, 0x03, //   Report Size (3)
32    0x95, 0x01, //   Report Count (1)
33    0x91, 0x01, //   Output (Constant)
34    // Key array (6 keys)
35    0x05, 0x07, //   Usage Page (Keyboard/Keypad)
36    0x19, 0x00, //   Usage Minimum (0)
37    0x29, 0xFF, //   Usage Maximum (255)
38    0x15, 0x00, //   Logical Minimum (0)
39    0x26, 0xFF, 0x00, //   Logical Maximum (255)
40    0x75, 0x08, //   Report Size (8)
41    0x95, 0x06, //   Report Count (6)
42    0x81, 0x00, //   Input (Data, Array)
43    0xC0, // End Collection
44];
45
46/// Modifier bits for [`KeyboardReport::modifiers`].
47pub mod modifier {
48    /// Left Control.
49    pub const LEFT_CTRL: u8 = 1 << 0;
50    /// Left Shift.
51    pub const LEFT_SHIFT: u8 = 1 << 1;
52    /// Left Alt.
53    pub const LEFT_ALT: u8 = 1 << 2;
54    /// Left GUI (Windows/Super).
55    pub const LEFT_GUI: u8 = 1 << 3;
56    /// Right Control.
57    pub const RIGHT_CTRL: u8 = 1 << 4;
58    /// Right Shift.
59    pub const RIGHT_SHIFT: u8 = 1 << 5;
60    /// Right Alt.
61    pub const RIGHT_ALT: u8 = 1 << 6;
62    /// Right GUI (Windows/Super).
63    pub const RIGHT_GUI: u8 = 1 << 7;
64}
65
66/// LED bits reported back by the host (the output report).
67pub mod led {
68    /// Num Lock is on.
69    pub const NUM_LOCK: u8 = 1 << 0;
70    /// Caps Lock is on.
71    pub const CAPS_LOCK: u8 = 1 << 1;
72    /// Scroll Lock is on.
73    pub const SCROLL_LOCK: u8 = 1 << 2;
74    /// Compose is active.
75    pub const COMPOSE: u8 = 1 << 3;
76    /// Kana is active.
77    pub const KANA: u8 = 1 << 4;
78}
79
80/// Boot keyboard input report.
81///
82/// A report with `keys` all zero and no modifiers means "all keys released".
83#[derive(Clone, Copy, PartialEq, Eq, Default, Debug)]
84#[cfg_attr(feature = "defmt", derive(defmt::Format))]
85pub struct KeyboardReport {
86    /// Modifier bits, see [`modifier`].
87    pub modifiers: u8,
88    /// Up to six simultaneous keycodes; a zero entry means "no key".
89    pub keys: [u8; 6],
90}
91
92impl KeyboardReport {
93    /// Report the keys as an 8-byte boot report.
94    pub fn to_bytes(self) -> [u8; 8] {
95        [
96            self.modifiers,
97            0,
98            self.keys[0],
99            self.keys[1],
100            self.keys[2],
101            self.keys[3],
102            self.keys[4],
103            self.keys[5],
104        ]
105    }
106}
107
108/// Boot-compatible keyboard.
109///
110/// Pressing a key means sending a report with that key set, then sending
111/// [`KeyboardReport::default`] (or the next set of keys) to release it.  Wrap it
112/// in a [`HidClass`](super::HidClass) to talk to the host:
113///
114/// ```ignore
115/// let mut kbd = HidClass::new(&alloc, Keyboard::new());
116/// kbd.send_input(&KeyboardReport { keys: [0x04, 0, 0, 0, 0, 0], ..Default::default() }.to_bytes());
117/// ```
118pub struct Keyboard {
119    leds: u8,
120}
121
122impl Keyboard {
123    /// Create a keyboard with all LEDs off.
124    pub const fn new() -> Self {
125        Self { leds: 0 }
126    }
127
128    /// LED state the host last asked for, see [`led`].
129    pub fn leds(&self) -> u8 {
130        self.leds
131    }
132}
133
134impl Default for Keyboard {
135    fn default() -> Self {
136        Self::new()
137    }
138}
139
140impl<B: UsbBus> HidDevice<B> for Keyboard {
141    fn spec(&self) -> HidSpec {
142        HidSpec::new(KEYBOARD_REPORT_DESCRIPTOR, 8)
143            .subclass(hid_subclass::BOOT)
144            .protocol(hid_protocol::KEYBOARD)
145            .out_report_size(1) // LED report
146            .poll_interval_ms(10)
147    }
148
149    fn output_report(&mut self, _report_id: u8, data: &[u8]) {
150        self.leds = data.first().copied().unwrap_or(0);
151    }
152
153    fn reset(&mut self) {
154        self.leds = 0;
155    }
156}