Created
May 6, 2018 18:56
-
-
Save MrMinimal/96ec491d0cd05a996f13e5adeca90ffd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate winit; | |
use winit::{ControlFlow, DeviceEvent, WindowEvent, ElementState, KeyboardInput}; | |
fn main() { | |
let mut events_loop = winit::EventsLoop::new(); | |
let window = winit::WindowBuilder::new().build(&events_loop).unwrap(); | |
window.set_title("winit - Cursor grabbing test"); | |
let mut grabbed = false; | |
events_loop.run_forever(|event| { | |
println!("{:?}", event); | |
match event { | |
winit::Event::WindowEvent { event, .. } => { | |
match event { | |
WindowEvent::KeyboardInput { input: KeyboardInput { state: 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"); | |
} | |
}, | |
WindowEvent::CloseRequested => return ControlFlow::Break, | |
a @ WindowEvent::CursorMoved { .. } => { | |
println!("{:?}", a); | |
}, | |
_ => (), | |
} | |
}, | |
winit::Event::DeviceEvent { event, .. } => { | |
match event { | |
DeviceEvent::MouseMotion { delta: (x, y) } => { | |
println!("==================================================="); | |
println!("\tRaw mouse movement by delta: ({},{})", x, y); | |
println!("==================================================="); | |
}, | |
_ => (), | |
} | |
} | |
_ => {} | |
} | |
ControlFlow::Continue | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment