Expand description

A Rust crate for translating keycodes based on Chrome’s mapping of keys.

Easily convert, generate, listen for, or map keycodes for Linux, Windows, Mac, USB, and browsers! Includes a struct to manage the state of pressed keys and generate USB HID reports. Can be used for #![no_std] crates.

Source of keycodes data:

Example: get a key mapping

use keycode::{KeyMap, KeyMappingId};

// Check the USB HID value of the "a" key
let a = KeyMap::from(KeyMappingId::UsA);
assert_eq!(a.usb, 0x0004);
assert_eq!(a.evdev, 0x001e);
assert_eq!(a.xkb, 0x0026);
assert_eq!(a.win, 0x001e);
assert_eq!(a.mac, 0x0000);

Example: generate a USB HID report

use keycode::{KeyboardState, KeyMap, KeyMappingId, KeyState};

// Press and release the "A" key
// Generate a keyboard state with n-key rollover
let mut keyboard_state = KeyboardState::new(None);

// Get key mappings
let a = KeyMap::from(KeyMappingId::UsA);
let shift = KeyMap::from(KeyMappingId::ShiftLeft);

// USB HID report for "no keys pressed"
assert_eq!(keyboard_state.usb_input_report(), &[0; 8]);

// Press "shift" and "a" keys
keyboard_state.update_key(a, KeyState::Pressed);
keyboard_state.update_key(shift, KeyState::Pressed);

// USB HID report for "'A' is pressed"
assert_eq!(
    keyboard_state.usb_input_report(),
    &[shift.modifier.unwrap().bits(), 0, a.usb as u8, 0, 0, 0, 0, 0]
);

// Release "shift" and "a" keys
keyboard_state.update_key(a, KeyState::Released);
keyboard_state.update_key(shift, KeyState::Released);

// USB HID report for "no keys pressed"
assert_eq!(keyboard_state.usb_input_report(), &[0; 8]);

Structs

Ergonomic access to a specific key’s mapping of values

Bitmask for key modifiers based on the USB HID standard

Keyboard state that helps manage pressed keys, rollover, and generating USB HID reports

Enums

The mapping of values between platforms for a specific key

W3 browser event code for a specific key

Id for a specific key

State of any key, whether it is pressed or not

Constants

Max keys is 235, but this is the size of array used to manage state