typing/
typing.rs

1use winit_input_map::*;
2use gilrs::Gilrs;
3use winit::{event::*, application::*, window::*, event_loop::*, keyboard::KeyCode};
4
5#[derive(Copy, Clone, PartialEq, Eq, Hash)]
6enum Action { Return }
7
8fn main() {
9    let input = input_map!((Action::Return, KeyCode::Enter));
10    
11    let gilrs = Gilrs::new().unwrap();
12    let event_loop = EventLoop::new().unwrap();
13    event_loop.run_app(&mut App { window: None, input, gilrs, text: String::new() }).unwrap();
14}
15
16struct App { window: Option<Window>, input: InputMap<Action>, gilrs: Gilrs, text: String }
17impl ApplicationHandler for App {
18    fn resumed(&mut self, event_loop: &ActiveEventLoop) {
19        self.window = Some(event_loop.create_window(Window::default_attributes()).unwrap());
20    }
21    fn window_event(&mut self, event_loop: &ActiveEventLoop, _: WindowId, event: WindowEvent) {
22        self.input.update_with_window_event(&event);
23        if let WindowEvent::CloseRequested = &event { event_loop.exit() }
24    }
25    fn device_event(&mut self, _: &ActiveEventLoop, id: DeviceId, event: DeviceEvent) {
26        self.input.update_with_device_event(id, &event);
27    }
28    fn about_to_wait(&mut self, _: &ActiveEventLoop) {
29        self.input.update_with_gilrs(&mut self.gilrs);
30
31        if self.input.pressed(Action::Return) {
32            println!("{}", self.text);
33            self.text = String::new();
34        } else if let Some(new) = &self.input.text_typed {
35            self.text.push_str(new);
36        }
37
38        self.input.init();
39    }
40}