1 use winit::{
2     event::{Event, KeyboardInput, WindowEvent},
3     event_loop::{ControlFlow, EventLoop},
4     window::WindowBuilder,
5 };
6 
main()7 fn main() {
8     simple_logger::init().unwrap();
9     let event_loop = EventLoop::new();
10 
11     let _window = WindowBuilder::new()
12         .with_title("Your faithful window")
13         .build(&event_loop)
14         .unwrap();
15 
16     let mut close_requested = false;
17 
18     event_loop.run(move |event, _, control_flow| {
19         use winit::event::{
20             ElementState::Released,
21             VirtualKeyCode::{N, Y},
22         };
23         *control_flow = ControlFlow::Wait;
24 
25         match event {
26             Event::WindowEvent { event, .. } => {
27                 match event {
28                     WindowEvent::CloseRequested => {
29                         // `CloseRequested` is sent when the close button on the window is pressed (or
30                         // through whatever other mechanisms the window manager provides for closing a
31                         // window). If you don't handle this event, the close button won't actually do
32                         // anything.
33 
34                         // A common thing to do here is prompt the user if they have unsaved work.
35                         // Creating a proper dialog box for that is far beyond the scope of this
36                         // example, so here we'll just respond to the Y and N keys.
37                         println!("Are you ready to bid your window farewell? [Y/N]");
38                         close_requested = true;
39 
40                         // In applications where you can safely close the window without further
41                         // action from the user, this is generally where you'd handle cleanup before
42                         // closing the window. How to close the window is detailed in the handler for
43                         // the Y key.
44                     }
45                     WindowEvent::KeyboardInput {
46                         input:
47                             KeyboardInput {
48                                 virtual_keycode: Some(virtual_code),
49                                 state: Released,
50                                 ..
51                             },
52                         ..
53                     } => {
54                         match virtual_code {
55                             Y => {
56                                 if close_requested {
57                                     // This is where you'll want to do any cleanup you need.
58                                     println!("Buh-bye!");
59 
60                                     // For a single-window application like this, you'd normally just
61                                     // break out of the event loop here. If you wanted to keep running the
62                                     // event loop (i.e. if it's a multi-window application), you need to
63                                     // drop the window. That closes it, and results in `Destroyed` being
64                                     // sent.
65                                     *control_flow = ControlFlow::Exit;
66                                 }
67                             }
68                             N => {
69                                 if close_requested {
70                                     println!("Your window will continue to stay by your side.");
71                                     close_requested = false;
72                                 }
73                             }
74                             _ => (),
75                         }
76                     }
77                     _ => (),
78                 }
79             }
80             _ => (),
81         }
82     });
83 }
84