Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221//! US-QWERTY character/key-name to Linux evdev keycode tables.//!//! Synthetic keyboard input is expressed as evdev keycodes (the values in//! `linux/input-event-codes.h`). The compositor's keyboard uses the default US xkb//! layout, so mapping a character to the keycode-plus-shift that produces it under that//! layout is a fixed table. This is display-independent and unit-tested directly.
/// Evdev keycode of the left Shift modifier (`KEY_LEFTSHIFT`).////// What: `pub const LEFT_SHIFT: u32 = 42;`. Unsigned 32-bit; the raw evdev code./// Why: Typing an uppercase or shifted character wraps the key tap in a Shift/// press/release, and the input layer needs this code.////// In TS you'd write (pseudocode):/// ```ts/// const LEFT_SHIFT = 42;/// ```pub const LEFT_SHIFT: u32 = 42;
/// Map a character to `(evdev_keycode, needs_shift)` under the US layout.////// What: `pub fn char_to_key(character: char) -> Option<(u32, bool)>`. Returns the/// keycode and whether Shift must be held, or `None` for characters not on a/// US keyboard (which the caller skips)./// Why: `type <text>` turns each character into a key tap; this is the lookup.////// In TS you'd write (pseudocode):/// ```ts/// function charToKey(character: string): [number, boolean] | undefined { ... }/// ```////// @example/// ```ts/// charToKey("A"); // => [30, true] (KEY_A held with Shift)/// charToKey("!"); // => [2, true] (KEY_1 held with Shift)/// ```pub fn char_to_key(character: char) -> Option<(u32, bool)> { // What: `if character.is_ascii_alphabetic() { ... }`. True for A-Z and a-z. // Why: Letters share one code per letter; case only sets the Shift flag. if character.is_ascii_alphabetic() { // What: `let code = letter_code(character.to_ascii_lowercase())?;`. Lowercase // the letter, look up its code; `?` returns `None` if somehow absent. // Why: The keycode is the same for `a` and `A`. let code = letter_code(character.to_ascii_lowercase())?;
// What: `Some((code, character.is_ascii_uppercase()))`. Shift is needed exactly // when the character is uppercase. Tail expression. // Why: Return the code plus whether to hold Shift. return Some((code, character.is_ascii_uppercase())); }
// What: `match character { ... }`. Map each non-letter character to its // `(code, shift)`. Digits and space are unshifted; the shifted symbols on // the number row and punctuation keys set `true`. Anything else is `None`. // Why: Cover the printable US-keyboard characters a test is likely to type. match character { ' ' => return Some((57, false)), '1' => return Some((2, false)), '!' => return Some((2, true)), '2' => return Some((3, false)), '@' => return Some((3, true)), '3' => return Some((4, false)), '#' => return Some((4, true)), '4' => return Some((5, false)), '$' => return Some((5, true)), '5' => return Some((6, false)), '%' => return Some((6, true)), '6' => return Some((7, false)), '^' => return Some((7, true)), '7' => return Some((8, false)), '&' => return Some((8, true)), '8' => return Some((9, false)), '*' => return Some((9, true)), '9' => return Some((10, false)), '(' => return Some((10, true)), '0' => return Some((11, false)), ')' => return Some((11, true)), '-' => return Some((12, false)), '_' => return Some((12, true)), '=' => return Some((13, false)), '+' => return Some((13, true)), '[' => return Some((26, false)), '{' => return Some((26, true)), ']' => return Some((27, false)), '}' => return Some((27, true)), '\\' => return Some((43, false)), '|' => return Some((43, true)), ';' => return Some((39, false)), ':' => return Some((39, true)), '\'' => return Some((40, false)), '"' => return Some((40, true)), '`' => return Some((41, false)), '~' => return Some((41, true)), ',' => return Some((51, false)), '<' => return Some((51, true)), '.' => return Some((52, false)), '>' => return Some((52, true)), '/' => return Some((53, false)), '?' => return Some((53, true)), _ => return None, }}
/// Map a lowercase ASCII letter to its evdev keycode.////// What: `fn letter_code(letter: char) -> Option<u32>`. Private helper covering/// `a`..=`z`./// Why: Keep the 26-entry letter table out of `char_to_key`'s symbol match.fn letter_code(letter: char) -> Option<u32> { // What: `match letter { 'a' => Some(30), ... }`. The QWERTY letter-to-code table. // Why: One authoritative place for letter keycodes. match letter { 'a' => return Some(30), 'b' => return Some(48), 'c' => return Some(46), 'd' => return Some(32), 'e' => return Some(18), 'f' => return Some(33), 'g' => return Some(34), 'h' => return Some(35), 'i' => return Some(23), 'j' => return Some(36), 'k' => return Some(37), 'l' => return Some(38), 'm' => return Some(50), 'n' => return Some(49), 'o' => return Some(24), 'p' => return Some(25), 'q' => return Some(16), 'r' => return Some(19), 's' => return Some(31), 't' => return Some(20), 'u' => return Some(22), 'v' => return Some(47), 'w' => return Some(17), 'x' => return Some(45), 'y' => return Some(21), 'z' => return Some(44), _ => return None, }}
/// Map a key name (`enter`, `space`, or a single character) to its evdev keycode.////// What: `pub fn named_key(name: &str) -> Option<u32>`. Returns the code for a named/// key, or for a one-character name delegates to `char_to_key` (dropping the/// Shift flag, since `key` presses a raw key). `None` for unknown names./// Why: The `key <name>` command presses named keys (Enter, arrows) and single/// characters alike.////// In TS you'd write (pseudocode):/// ```ts/// function namedKey(name: string): number | undefined { ... }/// ```////// @example/// ```ts/// namedKey("enter"); // => 28/// namedKey("a"); // => 30/// ```pub fn named_key(name: &str) -> Option<u32> { // What: `match name { ... }`. Named keys first; the `_` arm handles single // characters. Alternatives (`|`) accept common spellings. // Why: Accept both symbolic names and bare characters. match name { // Linux evdev modifier codes let press/release shortcuts stay on this compositor's isolated seat. "ctrl" => return Some(29), "shift" => return Some(42), "alt" => return Some(56), "meta" => return Some(125), "enter" | "return" => return Some(28), "escape" | "esc" => return Some(1), "tab" => return Some(15), "space" => return Some(57), "backspace" => return Some(14), "delete" | "del" => return Some(111), "up" => return Some(103), "down" => return Some(108), "left" => return Some(105), "right" => return Some(106), "home" => return Some(102), "end" => return Some(107), "pageup" => return Some(104), "pagedown" => return Some(109), _ => { // What: `let mut chars = name.chars();`. Iterator over the name's characters. // Why: Detect the single-character case. let mut chars = name.chars();
// What: `let first = chars.next()?;`. Take the first character, or `None` // (an empty name) via `?`. // Why: A single-character name maps through `char_to_key`. let first = chars.next()?;
// What: `if chars.next().is_some() { return None; }`. More than one // character and not a known name means unknown. // Why: Reject multi-character names we do not recognise. if chars.next().is_some() { return None; }
// What: `char_to_key(first).map(|(code, _shift)| code)`. Reuse the character // table, discarding the Shift flag. Tail expression. // Why: A bare character key press uses only the keycode. return char_to_key(first).map(|(code, _shift)| return code) } }}
/// What: `#[cfg(test)] #[path = "keymap_tests.rs"] mod tests;`. Declares the keymap/// unit test module from the sibling file./// Why: Keep the keycode-table tests beside the tables.////// In TS you'd write (pseudocode):/// ```ts/// import "./keymap_tests";/// ```#[cfg(test)]#[path = "keymap_tests.rs"]mod tests;