#winit #input #input-event #winit-window #state #helper #mouse-input

winit_input_helper

Processes winit events, allowing input state to be queried at any time

27 releases (15 breaking)

0.16.0 Mar 4, 2024
0.15.1 Nov 12, 2023
0.14.1 Mar 25, 2023
0.13.0 Aug 1, 2022
0.2.0 Nov 8, 2018

#87 in GUI

Download history 1274/week @ 2024-06-19 1062/week @ 2024-06-26 301/week @ 2024-07-03 902/week @ 2024-07-10 1066/week @ 2024-07-17 1150/week @ 2024-07-24 1186/week @ 2024-07-31 1314/week @ 2024-08-07 1210/week @ 2024-08-14 1102/week @ 2024-08-21 1251/week @ 2024-08-28 1158/week @ 2024-09-04 1154/week @ 2024-09-11 1113/week @ 2024-09-18 1559/week @ 2024-09-25 884/week @ 2024-10-02

4,841 downloads per month
Used in 53 crates (42 directly)

MIT license

28KB
446 lines

Winit Input Helper

Crates.io Docs

Processes and stores winit events, allowing input state to be queried at any time.

How to use

Each event is passed to the WinitInputHelper via the update method.

The current input state can then be accessed via methods such as key_pressed, key_released, key_held, mouse, mouse_diff etc.

To see all available methods look at docs.rs

use winit::event_loop::EventLoop;
use winit::keyboard::{Key, KeyCode};
use winit::window::WindowBuilder;
use winit_input_helper::WinitInputHelper;

fn main() {
    let mut input = WinitInputHelper::new();

    let event_loop = EventLoop::new().unwrap();
    let _window = WindowBuilder::new().build(&event_loop).unwrap();

    event_loop
        .run(move |event, elwt| {
            // Pass every event to the WinitInputHelper.
            // It will return true when the last event has been processed and it is time to run your application logic.
            if input.update(&event) {
                if input.key_released(KeyCode::KeyQ) || input.close_requested() || input.destroyed()
                {
                    elwt.exit();
                    return;
                }

                if input.key_pressed(KeyCode::KeyW) {
                    println!("The 'W' key (US layout) was pressed on the keyboard");
                }

                if input.key_held(KeyCode::KeyR) {
                    println!("The 'R' key (US layout) key is held");
                }

                // query the change in cursor this update
                let cursor_diff = input.cursor_diff();
                if cursor_diff != (0.0, 0.0) {
                    println!("The cursor diff is: {:?}", cursor_diff);
                    println!("The cursor position is: {:?}", input.cursor());
                }

                // You are expected to control your own timing within this block.
                // Usually via rendering with vsync.
                // render();
            }
        })
        .unwrap();
}

Publishing a new version

In order to avoid forcing the user to enable the default winit backends, winit_input_helper sets its winit dependency to default-features = false. This complicates the publishing procedure a little because winit cannot compile without any backends enabled.

So to publish we run: cargo publish --features winit/default

Dependencies

~2–19MB
~231K SLoC