Given an SDL `event`, converts it to a `Key` event if it is a key press; otherwise, returns `None` for unknown events.
(event: Event)
| 152 | /// Given an SDL `event`, converts it to a `Key` event if it is a key press; otherwise, returns |
| 153 | /// `None` for unknown events. |
| 154 | fn parse_event(event: Event) -> Option<Key> { |
| 155 | let ctrl_mods = Mod::LCTRLMOD | Mod::RCTRLMOD; |
| 156 | |
| 157 | match event { |
| 158 | Event::Quit { .. } => { |
| 159 | // TODO(jmmv): This isn't really a key so we should be handling it in some other way. |
| 160 | // For now, we recognize it here so that closing the window causes the interpreter to |
| 161 | // exit... but that only works when the interpreter is waiting for input (which means |
| 162 | // that this also confuses INKEY). |
| 163 | Some(Key::EofOrDelete) |
| 164 | } |
| 165 | |
| 166 | Event::KeyDown { keycode: Some(keycode), keymod, .. } => match keycode { |
| 167 | Keycode::A if keymod.intersects(ctrl_mods) => Some(Key::Home), |
| 168 | Keycode::B if keymod.intersects(ctrl_mods) => Some(Key::ArrowLeft), |
| 169 | Keycode::C if keymod.intersects(ctrl_mods) => Some(Key::Interrupt), |
| 170 | Keycode::D if keymod.intersects(ctrl_mods) => Some(Key::EofOrDelete), |
| 171 | Keycode::E if keymod.intersects(ctrl_mods) => Some(Key::End), |
| 172 | Keycode::F if keymod.intersects(ctrl_mods) => Some(Key::ArrowRight), |
| 173 | Keycode::J if keymod.intersects(ctrl_mods) => Some(Key::NewLine), |
| 174 | Keycode::M if keymod.intersects(ctrl_mods) => Some(Key::NewLine), |
| 175 | Keycode::N if keymod.intersects(ctrl_mods) => Some(Key::ArrowDown), |
| 176 | Keycode::P if keymod.intersects(ctrl_mods) => Some(Key::ArrowUp), |
| 177 | |
| 178 | Keycode::Backspace => Some(Key::Backspace), |
| 179 | Keycode::Delete => Some(Key::Delete), |
| 180 | Keycode::End => Some(Key::End), |
| 181 | Keycode::Escape => Some(Key::Escape), |
| 182 | Keycode::Home => Some(Key::Home), |
| 183 | Keycode::Return => Some(Key::NewLine), |
| 184 | Keycode::Tab => Some(Key::Tab), |
| 185 | |
| 186 | Keycode::Down => Some(Key::ArrowDown), |
| 187 | Keycode::Left => Some(Key::ArrowLeft), |
| 188 | Keycode::Right => Some(Key::ArrowRight), |
| 189 | Keycode::Up => Some(Key::ArrowUp), |
| 190 | |
| 191 | Keycode::PageDown => Some(Key::PageDown), |
| 192 | Keycode::PageUp => Some(Key::PageUp), |
| 193 | |
| 194 | _ => None, |
| 195 | }, |
| 196 | |
| 197 | Event::TextInput { text, .. } => { |
| 198 | let mut chars = text.chars(); |
| 199 | let first = |
| 200 | chars.next().unwrap_or_else(|| panic!("Cannot handle TextInput event: {:?}", text)); |
| 201 | Some(Key::Char(first)) |
| 202 | } |
| 203 | |
| 204 | _ => None, |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /// Implementation of the EndBASIC console on top of an SDL2 window. |
| 209 | /// |