|
| 1 | +//! Prints a keyboard as a Unicode graphic |
| 2 | +
|
| 3 | +use std::collections::HashMap; |
| 4 | + |
| 5 | +use pc_keyboard::{DecodedKey, KeyCode, KeyboardLayout, Modifiers}; |
| 6 | + |
| 7 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 8 | +enum KeyboardKind { |
| 9 | + Ansi, |
| 10 | + Iso, |
| 11 | +} |
| 12 | + |
| 13 | +fn main() { |
| 14 | + println!("Keyboard Layouts"); |
| 15 | + println!("================"); |
| 16 | + println!(); |
| 17 | + println!("Us104Key:"); |
| 18 | + show_kb(KeyboardKind::Ansi, &pc_keyboard::layouts::Us104Key); |
| 19 | + println!("Uk105Key:"); |
| 20 | + show_kb(KeyboardKind::Iso, &pc_keyboard::layouts::Uk105Key); |
| 21 | + println!("Colemak:"); |
| 22 | + show_kb(KeyboardKind::Ansi, &pc_keyboard::layouts::Colemak); |
| 23 | +} |
| 24 | + |
| 25 | +fn show_kb(kind: KeyboardKind, layout: &dyn KeyboardLayout) { |
| 26 | + let mut modifiers = Modifiers { |
| 27 | + lshift: false, |
| 28 | + rshift: false, |
| 29 | + lctrl: false, |
| 30 | + rctrl: false, |
| 31 | + numlock: true, |
| 32 | + capslock: false, |
| 33 | + lalt: false, |
| 34 | + ralt: false, |
| 35 | + rctrl2: false, |
| 36 | + }; |
| 37 | + println!("/// ## Unmodified"); |
| 38 | + show_kb_modifiers(kind, layout, &modifiers); |
| 39 | + |
| 40 | + modifiers.capslock = true; |
| 41 | + println!("/// ## Caps Lock"); |
| 42 | + show_kb_modifiers(kind, layout, &modifiers); |
| 43 | + modifiers.capslock = false; |
| 44 | + |
| 45 | + modifiers.lshift = true; |
| 46 | + println!("/// ## Shifted"); |
| 47 | + show_kb_modifiers(kind, layout, &modifiers); |
| 48 | + modifiers.lshift = false; |
| 49 | + |
| 50 | + modifiers.rctrl = true; |
| 51 | + println!("/// ## Control"); |
| 52 | + show_kb_modifiers(kind, layout, &modifiers); |
| 53 | + modifiers.rctrl = false; |
| 54 | + |
| 55 | + modifiers.ralt = true; |
| 56 | + println!("/// ## AltGr"); |
| 57 | + show_kb_modifiers(kind, layout, &modifiers); |
| 58 | + modifiers.ralt = false; |
| 59 | + |
| 60 | + modifiers.ralt = true; |
| 61 | + modifiers.lshift = true; |
| 62 | + println!("/// ## Shift AltGr"); |
| 63 | + show_kb_modifiers(kind, layout, &modifiers); |
| 64 | + modifiers.ralt = false; |
| 65 | + modifiers.lshift = false; |
| 66 | +} |
| 67 | + |
| 68 | +fn show_kb_modifiers(kind: KeyboardKind, layout: &dyn KeyboardLayout, modifiers: &Modifiers) { |
| 69 | + let mut map = Map::new(modifiers); |
| 70 | + map.insert("esc", KeyCode::Escape, layout); |
| 71 | + map.insert("oem8", KeyCode::Oem8, layout); |
| 72 | + map.insert("key1", KeyCode::Key1, layout); |
| 73 | + map.insert("key2", KeyCode::Key2, layout); |
| 74 | + map.insert("key3", KeyCode::Key3, layout); |
| 75 | + map.insert("key4", KeyCode::Key4, layout); |
| 76 | + map.insert("key5", KeyCode::Key5, layout); |
| 77 | + map.insert("key6", KeyCode::Key6, layout); |
| 78 | + map.insert("key7", KeyCode::Key7, layout); |
| 79 | + map.insert("key8", KeyCode::Key8, layout); |
| 80 | + map.insert("key9", KeyCode::Key9, layout); |
| 81 | + map.insert("key0", KeyCode::Key0, layout); |
| 82 | + map.insert("oem_minus", KeyCode::OemMinus, layout); |
| 83 | + map.insert("oem_plus", KeyCode::OemPlus, layout); |
| 84 | + map.insert("backspace", KeyCode::Backspace, layout); |
| 85 | + map.insert("numpad_divide", KeyCode::NumpadDivide, layout); |
| 86 | + map.insert("numpad_multiply", KeyCode::NumpadMultiply, layout); |
| 87 | + map.insert("numpad_subtract", KeyCode::NumpadSubtract, layout); |
| 88 | + map.insert("tab", KeyCode::Tab, layout); |
| 89 | + map.insert("oem4", KeyCode::Oem4, layout); |
| 90 | + map.insert("oem6", KeyCode::Oem6, layout); |
| 91 | + map.insert("oem7", KeyCode::Oem7, layout); |
| 92 | + map.insert("delete", KeyCode::Delete, layout); |
| 93 | + map.insert("numpad7", KeyCode::Numpad7, layout); |
| 94 | + map.insert("numpad8", KeyCode::Numpad8, layout); |
| 95 | + map.insert("numpad9", KeyCode::Numpad9, layout); |
| 96 | + map.insert("numpadl", KeyCode::NumpadAdd, layout); |
| 97 | + map.insert("oem1", KeyCode::Oem1, layout); |
| 98 | + map.insert("oem3", KeyCode::Oem3, layout); |
| 99 | + map.insert("enter", KeyCode::Return, layout); |
| 100 | + map.insert("numpad4", KeyCode::Numpad4, layout); |
| 101 | + map.insert("numpad5", KeyCode::Numpad5, layout); |
| 102 | + map.insert("numpad6", KeyCode::Numpad6, layout); |
| 103 | + map.insert("oem_comma", KeyCode::OemComma, layout); |
| 104 | + map.insert("oem_period", KeyCode::OemPeriod, layout); |
| 105 | + map.insert("oem2", KeyCode::Oem2, layout); |
| 106 | + map.insert("numpad1", KeyCode::Numpad1, layout); |
| 107 | + map.insert("numpad2", KeyCode::Numpad2, layout); |
| 108 | + map.insert("numpad3", KeyCode::Numpad3, layout); |
| 109 | + map.insert("numpade", KeyCode::NumpadEnter, layout); |
| 110 | + map.insert("space", KeyCode::Spacebar, layout); |
| 111 | + map.insert("numpad0", KeyCode::Numpad0, layout); |
| 112 | + map.insert("numpad_period", KeyCode::NumpadPeriod, layout); |
| 113 | + map.insert("q", KeyCode::Q, layout); |
| 114 | + map.insert("w", KeyCode::W, layout); |
| 115 | + map.insert("e", KeyCode::E, layout); |
| 116 | + map.insert("r", KeyCode::R, layout); |
| 117 | + map.insert("t", KeyCode::T, layout); |
| 118 | + map.insert("y", KeyCode::Y, layout); |
| 119 | + map.insert("u", KeyCode::U, layout); |
| 120 | + map.insert("i", KeyCode::I, layout); |
| 121 | + map.insert("o", KeyCode::O, layout); |
| 122 | + map.insert("p", KeyCode::P, layout); |
| 123 | + map.insert("a", KeyCode::A, layout); |
| 124 | + map.insert("s", KeyCode::S, layout); |
| 125 | + map.insert("d", KeyCode::D, layout); |
| 126 | + map.insert("f", KeyCode::F, layout); |
| 127 | + map.insert("g", KeyCode::G, layout); |
| 128 | + map.insert("h", KeyCode::H, layout); |
| 129 | + map.insert("j", KeyCode::J, layout); |
| 130 | + map.insert("k", KeyCode::K, layout); |
| 131 | + map.insert("l", KeyCode::L, layout); |
| 132 | + map.insert("z", KeyCode::Z, layout); |
| 133 | + map.insert("x", KeyCode::X, layout); |
| 134 | + map.insert("c", KeyCode::C, layout); |
| 135 | + map.insert("v", KeyCode::V, layout); |
| 136 | + map.insert("b", KeyCode::B, layout); |
| 137 | + map.insert("n", KeyCode::N, layout); |
| 138 | + map.insert("m", KeyCode::M, layout); |
| 139 | + if kind == KeyboardKind::Iso { |
| 140 | + map.insert("oem5", KeyCode::Oem5, layout); |
| 141 | + map.print_iso(); |
| 142 | + } else { |
| 143 | + map.print_ansi(); |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +struct Map { |
| 148 | + inner: HashMap<&'static str, char>, |
| 149 | + modifiers: Modifiers, |
| 150 | +} |
| 151 | + |
| 152 | +impl Map { |
| 153 | + fn new(modifiers: &Modifiers) -> Map { |
| 154 | + Map { |
| 155 | + inner: HashMap::new(), |
| 156 | + modifiers: modifiers.clone(), |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + fn insert(&mut self, label: &'static str, keycode: KeyCode, layout: &dyn KeyboardLayout) { |
| 161 | + match layout.map_keycode( |
| 162 | + keycode, |
| 163 | + &self.modifiers, |
| 164 | + pc_keyboard::HandleControl::MapLettersToUnicode, |
| 165 | + ) { |
| 166 | + DecodedKey::Unicode(c) => self.inner.insert(label, c), |
| 167 | + e => { |
| 168 | + panic!("Wanted unicode from {:?}, got {:?}!", keycode, e); |
| 169 | + } |
| 170 | + }; |
| 171 | + } |
| 172 | + |
| 173 | + fn get(&self, label: &'static str) -> String { |
| 174 | + let c = self.inner.get(label).unwrap(); |
| 175 | + let c_value = *c as u32; |
| 176 | + if c_value <= 32 || c_value == 0x7F { |
| 177 | + format!("{:04x}", c_value) |
| 178 | + } else { |
| 179 | + format!("{:^4}", c) |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + fn print_ansi(&self) { |
| 184 | + let es = self.get("esc"); |
| 185 | + let o8 = self.get("oem8"); |
| 186 | + let k1 = self.get("key1"); |
| 187 | + let k2 = self.get("key2"); |
| 188 | + let k3 = self.get("key3"); |
| 189 | + let k4 = self.get("key4"); |
| 190 | + let k5 = self.get("key5"); |
| 191 | + let k6 = self.get("key6"); |
| 192 | + let k7 = self.get("key7"); |
| 193 | + let k8 = self.get("key8"); |
| 194 | + let k9 = self.get("key9"); |
| 195 | + let k0 = self.get("key0"); |
| 196 | + let om = self.get("oem_minus"); |
| 197 | + let ol = self.get("oem_plus"); |
| 198 | + let bs = self.get("backspace"); |
| 199 | + let nd = self.get("numpad_divide"); |
| 200 | + let nm = self.get("numpad_multiply"); |
| 201 | + let ns = self.get("numpad_subtract"); |
| 202 | + let tb = self.get("tab"); |
| 203 | + let o4 = self.get("oem4"); |
| 204 | + let o6 = self.get("oem6"); |
| 205 | + let o7 = self.get("oem7"); |
| 206 | + let de = self.get("delete"); |
| 207 | + let n7 = self.get("numpad7"); |
| 208 | + let n8 = self.get("numpad8"); |
| 209 | + let n9 = self.get("numpad9"); |
| 210 | + let nl = self.get("numpadl"); |
| 211 | + let o1 = self.get("oem1"); |
| 212 | + let o3 = self.get("oem3"); |
| 213 | + let en = self.get("enter"); |
| 214 | + let n4 = self.get("numpad4"); |
| 215 | + let n5 = self.get("numpad5"); |
| 216 | + let n6 = self.get("numpad6"); |
| 217 | + let oc = self.get("oem_comma"); |
| 218 | + let op = self.get("oem_period"); |
| 219 | + let o2 = self.get("oem2"); |
| 220 | + let n1 = self.get("numpad1"); |
| 221 | + let n2 = self.get("numpad2"); |
| 222 | + let n3 = self.get("numpad3"); |
| 223 | + let ne = self.get("numpade"); |
| 224 | + let sp = self.get("space"); |
| 225 | + let n0 = self.get("numpad0"); |
| 226 | + let np = self.get("numpad_period"); |
| 227 | + |
| 228 | + let kq = self.get("q"); |
| 229 | + let kw = self.get("w"); |
| 230 | + let ke = self.get("e"); |
| 231 | + let kr = self.get("r"); |
| 232 | + let kt = self.get("t"); |
| 233 | + let ky = self.get("y"); |
| 234 | + let ku = self.get("u"); |
| 235 | + let ki = self.get("i"); |
| 236 | + let ko = self.get("o"); |
| 237 | + let kp = self.get("p"); |
| 238 | + let ka = self.get("a"); |
| 239 | + let ks = self.get("s"); |
| 240 | + let kd = self.get("d"); |
| 241 | + let kf = self.get("f"); |
| 242 | + let kg = self.get("g"); |
| 243 | + let kh = self.get("h"); |
| 244 | + let kj = self.get("j"); |
| 245 | + let kk = self.get("k"); |
| 246 | + let kl = self.get("l"); |
| 247 | + let kz = self.get("z"); |
| 248 | + let kx = self.get("x"); |
| 249 | + let kc = self.get("c"); |
| 250 | + let kv = self.get("v"); |
| 251 | + let kb = self.get("b"); |
| 252 | + let kn = self.get("n"); |
| 253 | + let km = self.get("m"); |
| 254 | + |
| 255 | + println!( |
| 256 | + r#"/// |
| 257 | +/// ```text |
| 258 | +/// ┌────┐ ┌────┬────┬────┬────┐ ┌────┬────┬────┬────┐ ┌────┬────┬────┬────┐ ┌────┬────┬────┐ |
| 259 | +/// │{es}│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ |
| 260 | +/// └────┘ └────┴────┴────┴────┘ └────┴────┴────┴────┘ └────┴────┴────┴────┘ └────┴────┴────┘ |
| 261 | +/// |
| 262 | +/// ┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬─────────┐ ┌────┬────┬────┐ ┌────┬────┬────┬────┐ |
| 263 | +/// │{o8}│{k1}│{k2}│{k3}│{k4}│{k5}│{k6}│{k7}│{k8}│{k9}│{k0}│{om}│{ol}│{bs} │ │ │ │ │ │ │{nd}│{nm}│{ns}│ |
| 264 | +/// ├────┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬────────┤ ├────┼────┼────┤ ├────┼────┼────┼────┤ |
| 265 | +/// │{tb} │{kq}│{kw}│{ke}│{kr}│{kt}│{ky}│{ku}│{ki}│{ko}│{kp}│{o4}│{o6}│ {o7} │ │{de}│ │ │ │{n7}│{n8}│{n9}│ │ |
| 266 | +/// ├─────┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴────────┤ └────┴────┴────┘ ├────┼────┼────┤{nl}│ |
| 267 | +/// │ │{ka}│{ks}│{kd}│{kf}│{kg}│{kh}│{kj}│{kk}│{kl}│{o1}│{o3}│ {en} │ │{n4}│{n5}│{n6}│ │ |
| 268 | +/// ├──────┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴────────────┤ ┌────┐ ├────┼────┼────┼────┤ |
| 269 | +/// │ │{kz}│{kx}│{kc}│{kv}│{kb}│{kn}│{km}│{oc}│{op}│{o2}│ │ │ │ │{n1}│{n2}│{n3}│ │ |
| 270 | +/// ├─────┬───┴─┬──┴──┬─┴────┴────┴────┴────┴────┴───┬┴────┼────┴┬──────┬──────┤ ┌────┼────┼────┐ ├────┴────┼────┤{ne}│ |
| 271 | +/// │ │ │ │ {sp} │ │ │ │ │ │ │ │ │ │{n0} │{np}│ │ |
| 272 | +/// └─────┴─────┴─────┴──────────────────────────────┴─────┴─────┴──────┴──────┘ └────┴────┴────┘ └─────────┴────┴────┘ |
| 273 | +/// ``` |
| 274 | +///"# |
| 275 | + ); |
| 276 | + } |
| 277 | + |
| 278 | + fn print_iso(&self) { |
| 279 | + let es = self.get("esc"); |
| 280 | + let o8 = self.get("oem8"); |
| 281 | + let k1 = self.get("key1"); |
| 282 | + let k2 = self.get("key2"); |
| 283 | + let k3 = self.get("key3"); |
| 284 | + let k4 = self.get("key4"); |
| 285 | + let k5 = self.get("key5"); |
| 286 | + let k6 = self.get("key6"); |
| 287 | + let k7 = self.get("key7"); |
| 288 | + let k8 = self.get("key8"); |
| 289 | + let k9 = self.get("key9"); |
| 290 | + let k0 = self.get("key0"); |
| 291 | + let om = self.get("oem_minus"); |
| 292 | + let ol = self.get("oem_plus"); |
| 293 | + let bs = self.get("backspace"); |
| 294 | + let nd = self.get("numpad_divide"); |
| 295 | + let nm = self.get("numpad_multiply"); |
| 296 | + let ns = self.get("numpad_subtract"); |
| 297 | + let tb = self.get("tab"); |
| 298 | + let o4 = self.get("oem4"); |
| 299 | + let o6 = self.get("oem6"); |
| 300 | + let o7 = self.get("oem7"); |
| 301 | + let de = self.get("delete"); |
| 302 | + let n7 = self.get("numpad7"); |
| 303 | + let n8 = self.get("numpad8"); |
| 304 | + let n9 = self.get("numpad9"); |
| 305 | + let nl = self.get("numpadl"); |
| 306 | + let o1 = self.get("oem1"); |
| 307 | + let o3 = self.get("oem3"); |
| 308 | + let en = self.get("enter"); |
| 309 | + let n4 = self.get("numpad4"); |
| 310 | + let n5 = self.get("numpad5"); |
| 311 | + let n6 = self.get("numpad6"); |
| 312 | + let oc = self.get("oem_comma"); |
| 313 | + let op = self.get("oem_period"); |
| 314 | + let o2 = self.get("oem2"); |
| 315 | + let n1 = self.get("numpad1"); |
| 316 | + let n2 = self.get("numpad2"); |
| 317 | + let n3 = self.get("numpad3"); |
| 318 | + let ne = self.get("numpade"); |
| 319 | + let sp = self.get("space"); |
| 320 | + let n0 = self.get("numpad0"); |
| 321 | + let np = self.get("numpad_period"); |
| 322 | + let o5 = self.get("oem5"); |
| 323 | + |
| 324 | + let kq = self.get("q"); |
| 325 | + let kw = self.get("w"); |
| 326 | + let ke = self.get("e"); |
| 327 | + let kr = self.get("r"); |
| 328 | + let kt = self.get("t"); |
| 329 | + let ky = self.get("y"); |
| 330 | + let ku = self.get("u"); |
| 331 | + let ki = self.get("i"); |
| 332 | + let ko = self.get("o"); |
| 333 | + let kp = self.get("p"); |
| 334 | + let ka = self.get("a"); |
| 335 | + let ks = self.get("s"); |
| 336 | + let kd = self.get("d"); |
| 337 | + let kf = self.get("f"); |
| 338 | + let kg = self.get("g"); |
| 339 | + let kh = self.get("h"); |
| 340 | + let kj = self.get("j"); |
| 341 | + let kk = self.get("k"); |
| 342 | + let kl = self.get("l"); |
| 343 | + let kz = self.get("z"); |
| 344 | + let kx = self.get("x"); |
| 345 | + let kc = self.get("c"); |
| 346 | + let kv = self.get("v"); |
| 347 | + let kb = self.get("b"); |
| 348 | + let kn = self.get("n"); |
| 349 | + let km = self.get("m"); |
| 350 | + |
| 351 | + println!( |
| 352 | + r#"/// |
| 353 | +/// ```text |
| 354 | +/// ┌────┐ ┌────┬────┬────┬────┐ ┌────┬────┬────┬────┐ ┌────┬────┬────┬────┐ ┌────┬────┬────┐ |
| 355 | +/// │{es}│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ |
| 356 | +/// └────┘ └────┴────┴────┴────┘ └────┴────┴────┴────┘ └────┴────┴────┴────┘ └────┴────┴────┘ |
| 357 | +/// |
| 358 | +/// ┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬─────────┐ ┌────┬────┬────┐ ┌────┬────┬────┬────┐ |
| 359 | +/// │{o8}│{k1}│{k2}│{k3}│{k4}│{k5}│{k6}│{k7}│{k8}│{k9}│{k0}│{om}│{ol}│{bs} │ │ │ │ │ │ │{nd}│{nm}│{ns}│ |
| 360 | +/// ├────┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬────────┤ ├────┼────┼────┤ ├────┼────┼────┼────┤ |
| 361 | +/// │{tb} │{kq}│{kw}│{ke}│{kr}│{kt}│{ky}│{ku}│{ki}│{ko}│{kp}│{o4}│{o6}│ {en} │ │{de}│ │ │ │{n7}│{n8}│{n9}│ │ |
| 362 | +/// ├─────┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┐ │ └────┴────┴────┘ ├────┼────┼────┤{nl}│ |
| 363 | +/// │ │{ka}│{ks}│{kd}│{kf}│{kg}│{kh}│{kj}│{kk}│{kl}│{o1}│{o3}│{o7}│ │ │{n4}│{n5}│{n6}│ │ |
| 364 | +/// ├────┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴────┴───────┤ ┌────┐ ├────┼────┼────┼────┤ |
| 365 | +/// │ │{o5}│{kz}│{kx}│{kc}│{kv}│{kb}│{kn}│{km}│{oc}│{op}│{o2}│ │ │ │ │{n1}│{n2}│{n3}│ │ |
| 366 | +/// ├────┴┬───┴─┬──┴──┬─┴────┴────┴────┴────┴────┴───┬┴────┼────┴┬──────┬──────┤ ┌────┼────┼────┐ ├────┴────┼────┤{ne}│ |
| 367 | +/// │ │ │ │ {sp} │ │ │ │ │ │ │ │ │ │{n0} │{np}│ │ |
| 368 | +/// └─────┴─────┴─────┴──────────────────────────────┴─────┴─────┴──────┴──────┘ └────┴────┴────┘ └─────────┴────┴────┘ |
| 369 | +/// ``` |
| 370 | +///"# |
| 371 | + ); |
| 372 | + } |
| 373 | +} |
0 commit comments