1 个不稳定版本
0.14.2 | 2023年5月12日 |
---|
#1120 在 数学
每月51次 下载
22KB
351 行
Winit 输入助手
处理和存储 winit 事件,允许在任何时候查询输入状态。
如何使用
每个事件都通过 WinitInputHelper
的 update
方法传递。
然后可以通过例如 key_pressed
、key_released
、key_held
、mouse
、mouse_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