winit-sonoma-fix/examples/grabbing.rs

43 lines
1.3 KiB
Rust
Raw Normal View History

2016-03-27 04:07:52 +11:00
extern crate winit;
2017-01-29 01:45:01 +11:00
use winit::{WindowEvent, ElementState};
fn main() {
2017-01-29 01:45:01 +11:00
let events_loop = winit::EventsLoop::new();
let window = winit::WindowBuilder::new().build(&events_loop).unwrap();
2016-03-27 04:07:52 +11:00
window.set_title("winit - Cursor grabbing test");
let mut grabbed = false;
2015-12-27 01:47:25 +11:00
2017-01-29 01:45:01 +11:00
events_loop.run_forever(|event| {
println!("{:?}", event);
2015-06-16 21:48:08 +10:00
match event {
2017-01-29 01:45:01 +11:00
winit::Event::WindowEvent { event, .. } => {
match event {
WindowEvent::KeyboardInput(ElementState::Pressed, _, _) => {
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 21:48:08 +10:00
2017-01-29 01:45:01 +11:00
WindowEvent::Closed => events_loop.interrupt(),
2015-12-27 01:47:25 +11:00
2017-01-29 01:45:01 +11:00
a @ WindowEvent::MouseMoved(_, _) => {
println!("{:?}", a);
},
2015-06-16 21:48:08 +10:00
2017-01-29 01:45:01 +11:00
_ => (),
}
},
2015-06-16 21:48:08 +10:00
}
2017-01-29 01:45:01 +11:00
});
}