Skip to main content

keyboard/
keyboard.rs

1static FILENAME: &str = "keyboard.bin";
2
3use hid_descriptor::*;
4use hid_types::id::usage::generic_desktop;
5use hid_types::id::usage::keyboard_keypad::KnownUsage;
6use hid_types::id::{KnownCollectionType, KnownUsagePage, ioflags};
7
8fn main() {
9    // This is the keyboard descriptor provided by the Device Class Definition
10    // for Human Interface Devices (HID) specification, Appendix B.
11    let bytes = [
12        usage_page(KnownUsagePage::GenericDesktop),
13        usage(generic_desktop::KnownUsage::Keyboard),
14        collection(KnownCollectionType::Application),
15        // 8 modifier keys (Left Ctrl/Shift/Alt/Gui, Right Ctrl/Shift/Alt/Gui)
16        // each get their own dedicated bit in the report.
17        report_size(1),
18        report_count(8),
19        usage_page(KnownUsagePage::KeyboardKeypad),
20        usage_minimum(KnownUsage::LeftControl),
21        usage_maximum(KnownUsage::RightGui),
22        logical_minimum(0),
23        logical_maximum(1),
24        input(ioflags::DATA_VARIABLE_ABSOLUTE),
25        // A padding byte in the input report, for legacy compatibility.
26        report_count(1),
27        report_size(8),
28        input(ioflags::CONSTANT),
29        // 5 status LEDs, and 3 padding bits.
30        report_count(5),
31        report_size(1),
32        usage_page(KnownUsagePage::Led),
33        usage_minimum(1u16),
34        usage_maximum(5u16),
35        output(ioflags::DATA_VARIABLE_ABSOLUTE),
36        report_count(1),
37        report_size(3),
38        output(ioflags::CONSTANT),
39        // An array of (up to) 6 active keys.
40        report_count(6),
41        report_size(8),
42        logical_minimum(0),
43        logical_maximum(255),
44        usage_page(KnownUsagePage::KeyboardKeypad),
45        usage_minimum(0u16),
46        usage_maximum(255u16),
47        input(ioflags::ARRAY),
48        end_collection(),
49    ];
50    println!("writing keyboard descriptor to {FILENAME}");
51    write_to_file(bytes, FILENAME).unwrap();
52}