#winit #input #events #helper #winit-window #scroll #time

winit_input_helper_temp

具有水平滚动的 winit_input_helper 临时版本

1 个不稳定版本

0.14.2 2023年5月12日

#1120数学

Download history 14/week @ 2024-04-03 2/week @ 2024-04-17 42/week @ 2024-04-24

每月51次 下载

MIT 许可证

22KB
351

Winit 输入助手

Crates.io Docs

处理和存储 winit 事件,允许在任何时候查询输入状态。

如何使用

每个事件都通过 WinitInputHelperupdate 方法传递。

然后可以通过例如 key_pressedkey_releasedkey_heldmousemouse_diff 等方法访问当前输入状态。

要查看所有可用方法,请参阅 docs.rs

use winit::event::VirtualKeyCode;
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
use winit_input_helper::WinitInputHelper;

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

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

    event_loop.run(move |event, _, control_flow| {
        // Pass every event to the WindowInputHelper.
        // It will return true when the last event has been processed and it is time to run your application logic.
        if input.update(&event) {
            // query keypresses this update
            if input.key_pressed_os(VirtualKeyCode::A) {
                println!("The 'A' key was pressed on the keyboard (OS repeating)");
            }

            if input.key_pressed(VirtualKeyCode::A) {
                println!("The 'A' key was pressed on the keyboard");
            }

            if input.key_released(VirtualKeyCode::Q) || input.close_requested() || input.destroyed() {
                *control_flow = ControlFlow::Exit;
                return;
            }

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

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

发布新版本

为了避免强制用户启用默认的 winit 后端,winit_input_helper 将其 winit 依赖项设置为 default-features = false。这稍微复杂了发布过程,因为 winit 在不启用任何后端的情况下无法编译。

因此,为了发布,我们运行: cargo publish --features winit/default

依赖关系

~2–15MB
~142K SLoC