example/
example.rs

1#[derive(PartialEq, Eq, Clone, Copy, Hash)]
2enum Actions {
3    Debug,
4    Left,
5    Right,
6    Click,
7    MouseR, MouseL, MouseU, MouseD,
8    ScrollU, ScrollD
9}
10use winit_input_map::*;
11use Actions::*;
12use gilrs::Gilrs;
13use winit::{event::*, application::*, window::*, event_loop::*};
14fn main() {
15    let mut input = { use base_input_codes::*; input_map!(
16        (Debug, Space, South),
17        (Left,  ArrowLeft,  KeyA, LeftStickLeft ),
18        (Right, ArrowRight, KeyD, LeftStickRight),
19        (Click, MouseButton::Left),
20        (MouseR, MouseMoveRight, RightStickRight),
21        (MouseL, MouseMoveLeft,  RightStickLeft ),
22        (MouseU, MouseMoveUp,    RightStickUp   ),
23        (MouseD, MouseMoveDown,  RightStickDown ),
24        (ScrollU, Equal, MouseScrollUp  ),
25        (ScrollD, Minus, MouseScrollDown)
26    ) };
27    input.mouse_scale = 1.0;
28    input.scroll_scale = 1.0;
29    
30    let gilrs = Gilrs::new().unwrap();
31    let event_loop = EventLoop::new().unwrap();
32    event_loop.run_app(&mut App { window: None, input, gilrs }).unwrap();
33}
34struct App { window: Option<Window>, input: InputMap<Actions>, gilrs: Gilrs }
35impl ApplicationHandler for App {
36    fn resumed(&mut self, event_loop: &ActiveEventLoop) {
37        let window_settings = Window::default_attributes();
38        let window = event_loop.create_window(window_settings);
39        self.window = Some(window.unwrap());
40    }
41    fn window_event(
42        &mut self, event_loop: &ActiveEventLoop,
43        _: WindowId, event: WindowEvent
44    ) {
45        self.input.update_with_window_event(&event);
46        if let WindowEvent::CloseRequested = &event { event_loop.exit() }
47    }
48    fn device_event(
49        &mut self, _: &ActiveEventLoop, id: DeviceId, event: DeviceEvent
50    ) {
51        self.input.update_with_device_event(id, &event);
52    }
53    fn about_to_wait(&mut self, _: &ActiveEventLoop) {
54        self.input.update_with_gilrs(&mut self.gilrs);
55
56        let input = &mut self.input;
57        let scroll = input.axis(ScrollU, ScrollD);
58    
59        if input.pressed(Debug) {
60            println!("pressed {:?}", input.get_binds().into_iter().find(|&(a, _)| a == Debug).map(|(_, v)| v))
61        }
62        if input.pressing(Right) || input.pressing(Left) {
63            println!("axis: {}", input.axis(Right, Left))
64        }
65
66        let mouse_move = input.dir(MouseL, MouseR, MouseU, MouseD);
67        if mouse_move != (0.0, 0.0) {
68            println!(
69                "mouse moved: {:?} and is now at {:?}",
70                mouse_move, input.mouse_pos
71            )
72        }
73        if input.released(Click) {
74            println!("released")
75        }
76        if scroll != 0.0 {
77            println!("scrolling {}", scroll);
78        }
79        if let Some(other) = input.recently_pressed {
80            println!("{other:?}");
81        }
82        std::thread::sleep(std::time::Duration::from_millis(100));
83        // reset input. use after your done with the input
84        input.init();
85    }
86}
87