Skip to content

Commit

Permalink
KeyCode::key_char
Browse files Browse the repository at this point in the history
  • Loading branch information
stepancheg committed Nov 22, 2023
1 parent 208ecb5 commit 35fd650
Showing 1 changed file with 244 additions and 0 deletions.
244 changes: 244 additions & 0 deletions crates/bevy_input/src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,250 @@ pub enum KeyCode {
Cut,
}

impl KeyCode {
/// Naive mapping of keycode to `char`.
///
/// This function ignores keyboard layout, for example:
/// * difference between US/UK layouts
/// * non-English layouts
/// * layouts of non-standard keyboards with unusual extra keys
///
/// When there's no obvious mapping (for example, for F1-F12 keys), `None` is returned.
pub fn key_char(self, shift: bool) -> Option<char> {
match (self, shift) {
(KeyCode::Key1, false) => Some('1'),
(KeyCode::Key1, true) => Some('!'),
(KeyCode::Key2, false) => Some('2'),
(KeyCode::Key2, true) => Some('@'),
(KeyCode::Key3, false) => Some('3'),
(KeyCode::Key3, true) => Some('#'),
(KeyCode::Key4, false) => Some('4'),
(KeyCode::Key4, true) => Some('$'),
(KeyCode::Key5, false) => Some('5'),
(KeyCode::Key5, true) => Some('%'),
(KeyCode::Key6, false) => Some('6'),
(KeyCode::Key6, true) => Some('^'),
(KeyCode::Key7, false) => Some('7'),
(KeyCode::Key7, true) => Some('&'),
(KeyCode::Key8, false) => Some('8'),
(KeyCode::Key8, true) => Some('*'),
(KeyCode::Key9, false) => Some('9'),
(KeyCode::Key9, true) => Some('('),
(KeyCode::Key0, false) => Some('0'),
(KeyCode::Key0, true) => Some(')'),
(KeyCode::A, false) => Some('a'),
(KeyCode::A, true) => Some('A'),
(KeyCode::B, false) => Some('b'),
(KeyCode::B, true) => Some('B'),
(KeyCode::C, false) => Some('c'),
(KeyCode::C, true) => Some('C'),
(KeyCode::D, false) => Some('d'),
(KeyCode::D, true) => Some('D'),
(KeyCode::E, false) => Some('e'),
(KeyCode::E, true) => Some('E'),
(KeyCode::F, false) => Some('f'),
(KeyCode::F, true) => Some('F'),
(KeyCode::G, false) => Some('g'),
(KeyCode::G, true) => Some('G'),
(KeyCode::H, false) => Some('h'),
(KeyCode::H, true) => Some('H'),
(KeyCode::I, false) => Some('i'),
(KeyCode::I, true) => Some('I'),
(KeyCode::J, false) => Some('j'),
(KeyCode::J, true) => Some('J'),
(KeyCode::K, false) => Some('k'),
(KeyCode::K, true) => Some('K'),
(KeyCode::L, false) => Some('l'),
(KeyCode::L, true) => Some('L'),
(KeyCode::M, false) => Some('m'),
(KeyCode::M, true) => Some('M'),
(KeyCode::N, false) => Some('n'),
(KeyCode::N, true) => Some('N'),
(KeyCode::O, false) => Some('o'),
(KeyCode::O, true) => Some('O'),
(KeyCode::P, false) => Some('p'),
(KeyCode::P, true) => Some('P'),
(KeyCode::Q, false) => Some('q'),
(KeyCode::Q, true) => Some('Q'),
(KeyCode::R, false) => Some('r'),
(KeyCode::R, true) => Some('R'),
(KeyCode::S, false) => Some('s'),
(KeyCode::S, true) => Some('S'),
(KeyCode::T, false) => Some('t'),
(KeyCode::T, true) => Some('T'),
(KeyCode::U, false) => Some('u'),
(KeyCode::U, true) => Some('U'),
(KeyCode::V, false) => Some('v'),
(KeyCode::V, true) => Some('V'),
(KeyCode::W, false) => Some('w'),
(KeyCode::W, true) => Some('W'),
(KeyCode::X, false) => Some('x'),
(KeyCode::X, true) => Some('X'),
(KeyCode::Y, false) => Some('y'),
(KeyCode::Y, true) => Some('Y'),
(KeyCode::Z, false) => Some('z'),
(KeyCode::Z, true) => Some('Z'),
(KeyCode::Escape, _) => None,
(KeyCode::F1, _) => None,
(KeyCode::F2, _) => None,
(KeyCode::F3, _) => None,
(KeyCode::F4, _) => None,
(KeyCode::F5, _) => None,
(KeyCode::F6, _) => None,
(KeyCode::F7, _) => None,
(KeyCode::F8, _) => None,
(KeyCode::F9, _) => None,
(KeyCode::F10, _) => None,
(KeyCode::F11, _) => None,
(KeyCode::F12, _) => None,
(KeyCode::F13, _) => None,
(KeyCode::F14, _) => None,
(KeyCode::F15, _) => None,
(KeyCode::F16, _) => None,
(KeyCode::F17, _) => None,
(KeyCode::F18, _) => None,
(KeyCode::F19, _) => None,
(KeyCode::F20, _) => None,
(KeyCode::F21, _) => None,
(KeyCode::F22, _) => None,
(KeyCode::F23, _) => None,
(KeyCode::F24, _) => None,
(KeyCode::Snapshot, _) => None,
(KeyCode::Scroll, _) => None,
(KeyCode::Pause, _) => None,
(KeyCode::Insert, _) => None,
(KeyCode::Home, _) => None,
(KeyCode::Delete, _) => None,
(KeyCode::End, _) => None,
(KeyCode::PageDown, _) => None,
(KeyCode::PageUp, _) => None,
(KeyCode::Left, _) => None,
(KeyCode::Up, _) => None,
(KeyCode::Right, _) => None,
(KeyCode::Down, _) => None,
(KeyCode::Back, _) => None,
(KeyCode::Return, _) => None,
(KeyCode::Space, _) => Some(' '),
(KeyCode::Compose, _) => None,
(KeyCode::Caret, _) => None,
(KeyCode::Numlock, _) => None,
(KeyCode::Numpad0, false) => Some('0'),
(KeyCode::Numpad0, true) => None,
(KeyCode::Numpad1, false) => Some('1'),
(KeyCode::Numpad1, true) => None,
(KeyCode::Numpad2, false) => Some('2'),
(KeyCode::Numpad2, true) => None,
(KeyCode::Numpad3, false) => Some('3'),
(KeyCode::Numpad3, true) => None,
(KeyCode::Numpad4, false) => Some('4'),
(KeyCode::Numpad4, true) => None,
(KeyCode::Numpad5, false) => Some('5'),
(KeyCode::Numpad5, true) => None,
(KeyCode::Numpad6, false) => Some('6'),
(KeyCode::Numpad6, true) => None,
(KeyCode::Numpad7, false) => Some('7'),
(KeyCode::Numpad7, true) => None,
(KeyCode::Numpad8, false) => Some('8'),
(KeyCode::Numpad8, true) => None,
(KeyCode::Numpad9, false) => Some('9'),
(KeyCode::Numpad9, true) => None,
(KeyCode::AbntC1, _) => None,
(KeyCode::AbntC2, _) => None,
(KeyCode::NumpadAdd, _) => None,
(KeyCode::Apostrophe, _) => None,
(KeyCode::Apps, _) => None,
(KeyCode::Asterisk, _) => Some('*'),
(KeyCode::Plus, false) => Some('+'),
(KeyCode::Plus, true) => Some('='),
(KeyCode::At, _) => None,
(KeyCode::Ax, _) => None,
(KeyCode::Backslash, false) => Some('\\'),
(KeyCode::Backslash, true) => Some('|'),
(KeyCode::Calculator, _) => None,
(KeyCode::Capital, _) => None,
(KeyCode::Colon, false) => Some(';'),
(KeyCode::Colon, true) => Some(':'),
(KeyCode::Comma, false) => Some(','),
(KeyCode::Comma, true) => Some('<'),
(KeyCode::Convert, _) => None,
(KeyCode::NumpadDecimal, false) => Some('.'),
(KeyCode::NumpadDecimal, true) => None,
(KeyCode::NumpadDivide, false) => Some('/'),
(KeyCode::NumpadDivide, true) => None,
(KeyCode::Equals, false) => Some('='),
(KeyCode::Equals, true) => Some('+'),
(KeyCode::Grave, false) => Some('`'),
(KeyCode::Grave, true) => Some('~'),
(KeyCode::Kana, _) => None,
(KeyCode::Kanji, _) => None,
(KeyCode::AltLeft, _) => None,
(KeyCode::BracketLeft, false) => Some('['),
(KeyCode::BracketLeft, true) => Some('{'),
(KeyCode::ControlLeft, _) => None,
(KeyCode::ShiftLeft, _) => None,
(KeyCode::SuperLeft, _) => None,
(KeyCode::Mail, _) => None,
(KeyCode::MediaSelect, _) => None,
(KeyCode::MediaStop, _) => None,
(KeyCode::Minus, false) => Some('-'),
(KeyCode::Minus, true) => Some('_'),
(KeyCode::NumpadMultiply, false) => Some('*'),
(KeyCode::NumpadMultiply, true) => None,
(KeyCode::Mute, _) => None,
(KeyCode::MyComputer, _) => None,
(KeyCode::NavigateForward, _) => None,
(KeyCode::NavigateBackward, _) => None,
(KeyCode::NextTrack, _) => None,
(KeyCode::NoConvert, _) => None,
(KeyCode::NumpadComma, false) => Some(','),
(KeyCode::NumpadComma, true) => None,
(KeyCode::NumpadEnter, false) => Some('\n'),
(KeyCode::NumpadEnter, true) => None,
(KeyCode::NumpadEquals, false) => Some('='),
(KeyCode::NumpadEquals, true) => None,
(KeyCode::Oem102, _) => None,
(KeyCode::Period, false) => Some('.'),
(KeyCode::Period, true) => Some('>'),
(KeyCode::PlayPause, _) => None,
(KeyCode::Power, _) => None,
(KeyCode::PrevTrack, _) => None,
(KeyCode::AltRight, _) => None,
(KeyCode::BracketRight, false) => Some(']'),
(KeyCode::BracketRight, true) => Some('}'),
(KeyCode::ControlRight, _) => None,
(KeyCode::ShiftRight, _) => None,
(KeyCode::SuperRight, _) => None,
(KeyCode::Semicolon, false) => Some(';'),
(KeyCode::Semicolon, true) => Some(':'),
(KeyCode::Slash, false) => Some('/'),
(KeyCode::Slash, true) => Some('?'),
(KeyCode::Sleep, _) => None,
(KeyCode::Stop, _) => None,
(KeyCode::NumpadSubtract, false) => Some('-'),
(KeyCode::NumpadSubtract, true) => None,
(KeyCode::Sysrq, _) => None,
(KeyCode::Tab, false) => Some('\t'),
(KeyCode::Tab, true) => None,
(KeyCode::Underline, _) => None,
(KeyCode::Unlabeled, _) => None,
(KeyCode::VolumeDown, _) => None,
(KeyCode::VolumeUp, _) => None,
(KeyCode::Wake, _) => None,
(KeyCode::WebBack, _) => None,
(KeyCode::WebFavorites, _) => None,
(KeyCode::WebForward, _) => None,
(KeyCode::WebHome, _) => None,
(KeyCode::WebRefresh, _) => None,
(KeyCode::WebSearch, _) => None,
(KeyCode::WebStop, _) => None,
(KeyCode::Yen, _) => None,
(KeyCode::Copy, _) => None,
(KeyCode::Paste, _) => None,
(KeyCode::Cut, _) => None,
}
}
}

/// The scan code of a [`KeyboardInput`].
///
/// ## Usage
Expand Down

0 comments on commit 35fd650

Please sign in to comment.