mirror of
https://github.com/italicsjenga/winit-sonoma-fix.git
synced 2024-12-23 22:01:31 +11:00
d624a3e648
This allows downstream users to avoid importing `ControlFlow` from winit.
57 lines
2 KiB
Rust
57 lines
2 KiB
Rust
use simple_logger::SimpleLogger;
|
|
use winit::{
|
|
event::{DeviceEvent, ElementState, Event, KeyboardInput, ModifiersState, WindowEvent},
|
|
event_loop::EventLoop,
|
|
window::WindowBuilder,
|
|
};
|
|
|
|
fn main() {
|
|
SimpleLogger::new().init().unwrap();
|
|
let event_loop = EventLoop::new();
|
|
|
|
let window = WindowBuilder::new()
|
|
.with_title("Super Cursor Grab'n'Hide Simulator 9000")
|
|
.build(&event_loop)
|
|
.unwrap();
|
|
|
|
let mut modifiers = ModifiersState::default();
|
|
|
|
event_loop.run(move |event, _, control_flow| {
|
|
control_flow.set_wait();
|
|
|
|
match event {
|
|
Event::WindowEvent { event, .. } => match event {
|
|
WindowEvent::CloseRequested => control_flow.set_exit(),
|
|
WindowEvent::KeyboardInput {
|
|
input:
|
|
KeyboardInput {
|
|
state: ElementState::Released,
|
|
virtual_keycode: Some(key),
|
|
..
|
|
},
|
|
..
|
|
} => {
|
|
use winit::event::VirtualKeyCode::*;
|
|
match key {
|
|
Escape => control_flow.set_exit(),
|
|
G => window.set_cursor_grab(!modifiers.shift()).unwrap(),
|
|
H => window.set_cursor_visible(modifiers.shift()),
|
|
_ => (),
|
|
}
|
|
}
|
|
WindowEvent::ModifiersChanged(m) => modifiers = m,
|
|
_ => (),
|
|
},
|
|
Event::DeviceEvent { event, .. } => match event {
|
|
DeviceEvent::MouseMotion { delta } => println!("mouse moved: {:?}", delta),
|
|
DeviceEvent::Button { button, state } => match state {
|
|
ElementState::Pressed => println!("mouse button {} pressed", button),
|
|
ElementState::Released => println!("mouse button {} released", button),
|
|
},
|
|
_ => (),
|
|
},
|
|
_ => (),
|
|
}
|
|
});
|
|
}
|