lesavka/client/src/input/keymap.rs

100 lines
3.4 KiB
Rust
Raw Normal View History

2025-06-08 04:11:58 -05:00
// client/src/input/keymap.rs
use evdev::KeyCode;
/// Return Some(usage) if we have a known mapping from evdev::KeyCode -> HID usage code
pub fn keycode_to_usage(key: KeyCode) -> Option<u8> {
match key {
// Letters
KeyCode::KEY_A => Some(0x04),
KeyCode::KEY_B => Some(0x05),
KeyCode::KEY_C => Some(0x06),
KeyCode::KEY_D => Some(0x07),
KeyCode::KEY_E => Some(0x08),
KeyCode::KEY_F => Some(0x09),
KeyCode::KEY_G => Some(0x0A),
KeyCode::KEY_H => Some(0x0B),
KeyCode::KEY_I => Some(0x0C),
KeyCode::KEY_J => Some(0x0D),
KeyCode::KEY_K => Some(0x0E),
KeyCode::KEY_L => Some(0x0F),
KeyCode::KEY_M => Some(0x10),
KeyCode::KEY_N => Some(0x11),
KeyCode::KEY_O => Some(0x12),
KeyCode::KEY_P => Some(0x13),
KeyCode::KEY_Q => Some(0x14),
KeyCode::KEY_R => Some(0x15),
KeyCode::KEY_S => Some(0x16),
KeyCode::KEY_T => Some(0x17),
KeyCode::KEY_U => Some(0x18),
KeyCode::KEY_V => Some(0x19),
KeyCode::KEY_W => Some(0x1A),
KeyCode::KEY_X => Some(0x1B),
KeyCode::KEY_Y => Some(0x1C),
KeyCode::KEY_Z => Some(0x1D),
// Number row
KeyCode::KEY_1 => Some(0x1E),
KeyCode::KEY_2 => Some(0x1F),
KeyCode::KEY_3 => Some(0x20),
KeyCode::KEY_4 => Some(0x21),
KeyCode::KEY_5 => Some(0x22),
KeyCode::KEY_6 => Some(0x23),
KeyCode::KEY_7 => Some(0x24),
KeyCode::KEY_8 => Some(0x25),
KeyCode::KEY_9 => Some(0x26),
KeyCode::KEY_0 => Some(0x27),
// Common punctuation
KeyCode::KEY_ENTER => Some(0x28),
KeyCode::KEY_ESC => Some(0x29),
KeyCode::KEY_BACKSPACE => Some(0x2A),
KeyCode::KEY_TAB => Some(0x2B),
KeyCode::KEY_SPACE => Some(0x2C),
KeyCode::KEY_MINUS => Some(0x2D),
KeyCode::KEY_EQUAL => Some(0x2E),
KeyCode::KEY_LEFTBRACE => Some(0x2F),
KeyCode::KEY_RIGHTBRACE => Some(0x30),
KeyCode::KEY_BACKSLASH => Some(0x31),
KeyCode::KEY_SEMICOLON => Some(0x33),
KeyCode::KEY_APOSTROPHE => Some(0x34),
KeyCode::KEY_GRAVE => Some(0x35),
KeyCode::KEY_COMMA => Some(0x36),
KeyCode::KEY_DOT => Some(0x37),
KeyCode::KEY_SLASH => Some(0x38),
// Function keys
KeyCode::KEY_CAPSLOCK => Some(0x39),
KeyCode::KEY_F1 => Some(0x3A),
KeyCode::KEY_F2 => Some(0x3B),
KeyCode::KEY_F3 => Some(0x3C),
KeyCode::KEY_F4 => Some(0x3D),
KeyCode::KEY_F5 => Some(0x3E),
KeyCode::KEY_F6 => Some(0x3F),
KeyCode::KEY_F7 => Some(0x40),
KeyCode::KEY_F8 => Some(0x41),
KeyCode::KEY_F9 => Some(0x42),
KeyCode::KEY_F10 => Some(0x43),
KeyCode::KEY_F11 => Some(0x44),
KeyCode::KEY_F12 => Some(0x45),
// We'll handle modifiers (ctrl, shift, alt, meta) in `is_modifier()`
_ => None,
}
}
/// If a key is a modifier, return the bit(s) to set in HID byte[0].
pub fn is_modifier(key: KeyCode) -> Option<u8> {
match key {
KeyCode::KEY_LEFTCTRL => Some(0x01),
KeyCode::KEY_LEFTSHIFT => Some(0x02),
KeyCode::KEY_LEFTALT => Some(0x04),
KeyCode::KEY_LEFTMETA => Some(0x08),
KeyCode::KEY_RIGHTCTRL => Some(0x10),
KeyCode::KEY_RIGHTSHIFT => Some(0x20),
KeyCode::KEY_RIGHTALT => Some(0x40),
KeyCode::KEY_RIGHTMETA => Some(0x80),
_ => None,
}
}