winit-sonoma-fix/examples/grabbing.rs

46 lines
1.4 KiB
Rust
Raw Normal View History

2016-03-26 18:07:52 +01:00
extern crate winit;
use winit::{ControlFlow, WindowEvent, ElementState, KeyboardInput};
fn main() {
let mut events_loop = winit::EventsLoop::new();
2017-01-28 15:45:01 +01:00
let window = winit::WindowBuilder::new().build(&events_loop).unwrap();
2016-03-26 18:07:52 +01:00
window.set_title("winit - Cursor grabbing test");
let mut grabbed = false;
2015-12-26 16:47:25 +02:00
2017-01-28 15:45:01 +01:00
events_loop.run_forever(|event| {
println!("{:?}", event);
2015-06-16 13:48:08 +02:00
match event {
2017-01-28 15:45:01 +01:00
winit::Event::WindowEvent { event, .. } => {
match event {
WindowEvent::KeyboardInput { input: KeyboardInput { state: ElementState::Pressed, .. }, .. } => {
2017-01-28 15:45:01 +01:00
if grabbed {
grabbed = false;
window.set_cursor_state(winit::CursorState::Normal)
.ok().expect("could not ungrab mouse cursor");
} else {
grabbed = true;
window.set_cursor_state(winit::CursorState::Grab)
.ok().expect("could not grab mouse cursor");
}
},
2015-06-16 13:48:08 +02:00
WindowEvent::Closed => return ControlFlow::Break,
2015-12-26 16:47:25 +02:00
a @ WindowEvent::CursorMoved { .. } => {
2017-01-28 15:45:01 +01:00
println!("{:?}", a);
},
2015-06-16 13:48:08 +02:00
2017-01-28 15:45:01 +01:00
_ => (),
}
}
_ => {}
2015-06-16 13:48:08 +02:00
}
ControlFlow::Continue
2017-01-28 15:45:01 +01:00
});
}