1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
extern crate glutin;

mod support;

use glutin::GlContext;

fn main() {
    let mut events_loop = glutin::EventsLoop::new();
    let window = glutin::WindowBuilder::new().with_title("glutin - Cursor grabbing test");
    let context = glutin::ContextBuilder::new();
    let gl_window = glutin::GlWindow::new(window, context, &events_loop).unwrap();

    let _ = unsafe { gl_window.make_current() };

    let gl = support::load(&gl_window);
    let mut grabbed = false;

    events_loop.run_forever(|event| {
        use glutin::{CursorState, ControlFlow, Event, WindowEvent, ElementState};
        match event {
            Event::WindowEvent { event, .. } => {
                match event {

                    WindowEvent::KeyboardInput { input, .. }
                        if ElementState::Pressed == input.state => {
                        if grabbed {
                            grabbed = false;
                            gl_window
                                .set_cursor_state(CursorState::Normal)
                                .ok()
                                .expect("could not ungrab mouse cursor");
                        } else {
                            grabbed = true;
                            gl_window.set_cursor_state(CursorState::Grab).ok().expect(
                                "could not grab mouse cursor",
                            );
                        }
                    }

                    WindowEvent::CloseRequested => return ControlFlow::Break,
                    WindowEvent::Resized(w, h) => gl_window.resize(w, h),
                    a @ WindowEvent::CursorMoved { .. } => {
                        println!("{:?}", a);
                    }
                    _ => (),
                }
            }
            _ => (),
        }

        gl.draw_frame([0.0, 1.0, 0.0, 1.0]);
        let _ = gl_window.swap_buffers();
        ControlFlow::Continue
    });
}