diff --git a/egui/src/input.rs b/egui/src/input.rs index 4271c32d..ba85ec12 100644 --- a/egui/src/input.rs +++ b/egui/src/input.rs @@ -235,44 +235,95 @@ pub enum Event { /// State of the modifier keys. These must be fed to Egui. #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] pub struct Modifiers { - /// Either of the alt keys are down (option ⌥ on Mac) + /// Either of the alt keys are down (option ⌥ on Mac). pub alt: bool, - /// Either of the control keys are down + /// Either of the control keys are down. + /// When checking for keyboard shortcuts, consider using [`Self::command`] instead. pub ctrl: bool, - /// Either of the shift keys are down + /// Either of the shift keys are down. pub shift: bool, /// The Mac ⌘ Command key. Should always be set to `false` on other platforms. pub mac_cmd: bool, - /// On Mac, this should be set whenever one of the ⌘ Command keys are down (same as `mac_cmd`). /// On Windows and Linux, set this to the same value as `ctrl`. + /// On Mac, this should be set whenever one of the ⌘ Command keys are down (same as `mac_cmd`). /// This is so that Egui can, for instance, select all text by checking for `command + A` /// and it will work on both Mac and Windows. pub command: bool, } -/// Keyboard key name. Only covers keys used by Egui (mostly for text editing). +/// Keyboard keys. +/// +/// Includes all keys Egui is interested in (such as `Home` and `End`) +/// plus a few that are useful for detecting keyboard shortcuts. +/// +/// Many keys are omitted because they are not always physical keys (depending on keyboard language), e.g. `;` and `§`, +/// and are therefor unsuitable as keyboard shortcuts if you want your app to be portable. #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)] pub enum Key { ArrowDown, ArrowLeft, ArrowRight, ArrowUp, + + Escape, + Tab, Backspace, - Delete, - End, Enter, Space, - Escape, - Home, + Insert, - PageDown, + Delete, + Home, + End, PageUp, - Tab, + PageDown, + + /// Either from the main row or from the numpad. + Num0, + /// Either from the main row or from the numpad. + Num1, + /// Either from the main row or from the numpad. + Num2, + /// Either from the main row or from the numpad. + Num3, + /// Either from the main row or from the numpad. + Num4, + /// Either from the main row or from the numpad. + Num5, + /// Either from the main row or from the numpad. + Num6, + /// Either from the main row or from the numpad. + Num7, + /// Either from the main row or from the numpad. + Num8, + /// Either from the main row or from the numpad. + Num9, A, // Used for cmd+A (select All) + B, + C, + D, + E, + F, + G, + H, + I, + J, K, // Used for ctrl+K (delete text after cursor) + L, + M, + N, + O, + P, + Q, + R, + S, + T, U, // Used for ctrl+U (delete text before cursor) + V, W, // Used for ctrl+W (delete previous word) + X, + Y, Z, // Used for cmd+Z (undo) } diff --git a/egui_glium/src/lib.rs b/egui_glium/src/lib.rs index 8ec45e11..3dd38ec6 100644 --- a/egui_glium/src/lib.rs +++ b/egui_glium/src/lib.rs @@ -165,26 +165,60 @@ pub fn translate_virtual_key_code(key: VirtualKeyCode) -> Option { use VirtualKeyCode::*; Some(match key { - Escape => Key::Escape, - Insert => Key::Insert, - Home => Key::Home, - Delete => Key::Delete, - End => Key::End, - PageDown => Key::PageDown, - PageUp => Key::PageUp, - Left => Key::ArrowLeft, - Up => Key::ArrowUp, - Right => Key::ArrowRight, Down => Key::ArrowDown, + Left => Key::ArrowLeft, + Right => Key::ArrowRight, + Up => Key::ArrowUp, + + Escape => Key::Escape, + Tab => Key::Tab, Back => Key::Backspace, Return => Key::Enter, - Tab => Key::Tab, Space => Key::Space, + Insert => Key::Insert, + Delete => Key::Delete, + Home => Key::Home, + End => Key::End, + PageUp => Key::PageUp, + PageDown => Key::PageDown, + + Key0 | Numpad0 => Key::Num0, + Key1 | Numpad1 => Key::Num1, + Key2 | Numpad2 => Key::Num2, + Key3 | Numpad3 => Key::Num3, + Key4 | Numpad4 => Key::Num4, + Key5 | Numpad5 => Key::Num5, + Key6 | Numpad6 => Key::Num6, + Key7 | Numpad7 => Key::Num7, + Key8 | Numpad8 => Key::Num8, + Key9 | Numpad9 => Key::Num9, + A => Key::A, + B => Key::B, + C => Key::C, + D => Key::D, + E => Key::E, + F => Key::F, + G => Key::G, + H => Key::H, + I => Key::I, + J => Key::J, K => Key::K, + L => Key::L, + M => Key::M, + N => Key::N, + O => Key::O, + P => Key::P, + Q => Key::Q, + R => Key::R, + S => Key::S, + T => Key::T, U => Key::U, + V => Key::V, W => Key::W, + X => Key::X, + Y => Key::Y, Z => Key::Z, _ => { diff --git a/egui_web/src/lib.rs b/egui_web/src/lib.rs index e1fbc2e6..ad0c2a26 100644 --- a/egui_web/src/lib.rs +++ b/egui_web/src/lib.rs @@ -319,22 +319,58 @@ pub fn translate_key(key: &str) -> Option { "ArrowLeft" => Some(egui::Key::ArrowLeft), "ArrowRight" => Some(egui::Key::ArrowRight), "ArrowUp" => Some(egui::Key::ArrowUp), + + "Esc" | "Escape" => Some(egui::Key::Escape), + "Tab" => Some(egui::Key::Tab), "Backspace" => Some(egui::Key::Backspace), - "Delete" => Some(egui::Key::Delete), - "End" => Some(egui::Key::End), "Enter" => Some(egui::Key::Enter), "Space" => Some(egui::Key::Space), - "Esc" | "Escape" => Some(egui::Key::Escape), + "Help" | "Insert" => Some(egui::Key::Insert), + "Delete" => Some(egui::Key::Delete), "Home" => Some(egui::Key::Home), - "PageDown" => Some(egui::Key::PageDown), + "End" => Some(egui::Key::End), "PageUp" => Some(egui::Key::PageUp), - "Tab" => Some(egui::Key::Tab), + "PageDown" => Some(egui::Key::PageDown), + + "0" => Some(egui::Key::Num0), + "1" => Some(egui::Key::Num1), + "2" => Some(egui::Key::Num2), + "3" => Some(egui::Key::Num3), + "4" => Some(egui::Key::Num4), + "5" => Some(egui::Key::Num5), + "6" => Some(egui::Key::Num6), + "7" => Some(egui::Key::Num7), + "8" => Some(egui::Key::Num8), + "9" => Some(egui::Key::Num9), + "a" | "A" => Some(egui::Key::A), + "b" | "B" => Some(egui::Key::B), + "c" | "C" => Some(egui::Key::C), + "d" | "D" => Some(egui::Key::D), + "e" | "E" => Some(egui::Key::E), + "f" | "F" => Some(egui::Key::F), + "g" | "G" => Some(egui::Key::G), + "h" | "H" => Some(egui::Key::H), + "i" | "I" => Some(egui::Key::I), + "j" | "J" => Some(egui::Key::J), "k" | "K" => Some(egui::Key::K), + "l" | "L" => Some(egui::Key::L), + "m" | "M" => Some(egui::Key::M), + "n" | "N" => Some(egui::Key::N), + "o" | "O" => Some(egui::Key::O), + "p" | "P" => Some(egui::Key::P), + "q" | "Q" => Some(egui::Key::Q), + "r" | "R" => Some(egui::Key::R), + "s" | "S" => Some(egui::Key::S), + "t" | "T" => Some(egui::Key::T), "u" | "U" => Some(egui::Key::U), + "v" | "V" => Some(egui::Key::V), "w" | "W" => Some(egui::Key::W), + "x" | "X" => Some(egui::Key::X), + "y" | "Y" => Some(egui::Key::Y), "z" | "Z" => Some(egui::Key::Z), + _ => None, } }