8 个版本

0.2.5 2024 年 6 月 30 日
0.2.4 2024 年 6 月 18 日
0.2.3 2024 年 5 月 10 日
0.1.1 2024 年 4 月 25 日

#781硬件支持

每月 50 次下载
用于 thin-engine

Apache-2.0 OR MIT

19KB
180

为 winit 提供处理鼠标和键盘按钮、鼠标位置和移动以及滚轮的输入映射。

#[derive(ToUsize)]
enum Actions {
    Debug,
    Left,
    Right,
    Click,
}
use winit_input_map::*;
use Actions::*;

let mut input = input_map!(
    (Debug, KeyCode::Space),
    (Left,  KeyCode::ArrowLeft, KeyCode::KeyA),
    (Right, KeyCode::ArrowRight, KeyCode::KeyD),
    (Click, MouseButton::Left)
);

// winit event handler
input.update(&event);

// use the input map!

// end of using event handler
input.init();

更完整的示例

fn main() {
    #[derive(ToUsize)]
    enum Actions {
        Debug,
        Left,
        Right,
        Click,
    }
    use winit_input_map::*;
    use Actions::*;

    let mut input = input_map!(
        (Debug, KeyCode::Space),
        (Left,  KeyCode::ArrowLeft, KeyCode::KeyA),
        (Right, KeyCode::ArrowRight, KeyCode::KeyD),
        (Click, MouseButton::Left)
    );

    use winit::{event::*, keyboard::KeyCode, window::Window};
    let event_loop = winit::event_loop::EventLoop::new().unwrap();
    event_loop.set_control_flow(winit::event_loop::ControlFlow::Poll);
    let _window = Window::new(&event_loop).unwrap();
    
    event_loop.run(|event, target| {
        input.update(&event);
        match &event {
            Event::WindowEvent { 
                event: WindowEvent::CloseRequested, ..
            } => target.exit(),
            Event::AboutToWait => {
                if input.pressed(Debug) {
                    println!("pressed {:?}", input.binds(Debug))
                }
                if input.pressing(Right) || input.pressing(Left) {
                    println!("axis: {}", input.axis(Right, Left))
                }
                if input.mouse_move != (0.0, 0.0) {
                    println!(
                        "mouse moved: {:?} and is now at {:?}",
                        input.mouse_move, input.mouse_pos
                    )
                }
                if input.released(Click) {
                    println!("released {:?}", input.binds(Click))
                }
                if input.mouse_scroll != 0.0 {
                    println!("scrolling {}", input.mouse_scroll);
                }
               std::thread::sleep(std::time::Duration::from_millis(100));
                //reset input. use after your done with the input
                input.init();
            }
            _ => (),
        }
    }).unwrap();
}

然而,上面的示例在 sway 中无法读取输入,因为没有渲染图形。

依赖项

~3–19MB
~245K SLoC