winiteventhelper is a crate for simplified winit event handling using callback functions without taking over the main loop.
winiteventhelper comes with the EventHelper
struct, which handles all the callbacks
and various miscellaneous things.
Pass your events to EventHelper::update
and run your application logic when it returns true
.
You can also add callbacks for specific winit events with the EventHelper helper functions
or the Callbacks
struct.
```rust use winit::eventloop::{ControlFlow, EventLoop}; use winit::window::WindowBuilder; use winitevent_helper::*;
struct Data { counter: usize, }
fn main() { let eventloop = EventLoop::new(); let _window = WindowBuilder::new().build(&eventloop).unwrap(); let mut eh = EventHelper::new(Data { counter: 0 }); let mut callbacks = Callbacks::new();
// is called whenever one of the given inputs was just pressed
callbacks
.window
.inputs
.just_pressed([GenericInput::from(MouseButton::Left), KeyCode::Space.into()], |eh| {
eh.counter += 1
});
event_loop.run(move |event, _, control_flow| {
// feed the events to the `EventHelper` struct
// returns true when it receives `Event::MainEventsCleared`
if !eh.update(&callbacks, &event) {
return;
}
// exits the application when the key combination CTRL + ESC has been released
if eh.data.window.inputs.just_released_combination([KeyCode::Escape], Modifiers::CTRL) {
*control_flow = ControlFlow::Exit;
}
println!("{}", eh.counter);
// do stuff
})
} ```