From fbd3d03de9c2d7516c7a63da489c99f498b710df Mon Sep 17 00:00:00 2001 From: MultisampledNight Date: Fri, 17 Dec 2021 14:39:28 +0100 Subject: [PATCH] Add exit code example --- examples/exit_code.rs | 57 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 examples/exit_code.rs diff --git a/examples/exit_code.rs b/examples/exit_code.rs new file mode 100644 index 000000000..be1cc31d2 --- /dev/null +++ b/examples/exit_code.rs @@ -0,0 +1,57 @@ +use { + simple_logger::SimpleLogger, + winit::{ + event::{Event, KeyboardInput, VirtualKeyCode, WindowEvent}, + event_loop::{ControlFlow, EventLoop}, + window::WindowBuilder, + }, +}; + +fn main() { + SimpleLogger::new().init().unwrap(); + let event_loop = EventLoop::new(); + + let _window = WindowBuilder::new() + .with_title("Highly unstable window") + .build(&event_loop) + .unwrap(); + + println!("Use your number keys (0-9) to enter an exit code!"); + + event_loop.run(move |event, _, flow| { + *flow = ControlFlow::Wait; + + match event { + Event::WindowEvent { + event: + WindowEvent::KeyboardInput { + input: + KeyboardInput { + virtual_keycode: Some(keycode), + .. + }, + .. + }, + .. + } => match keycode { + // actually perfect for a macro, but for simplicity I'll just refrain from that + VirtualKeyCode::Key1 => *flow = ControlFlow::Exit(1), + VirtualKeyCode::Key2 => *flow = ControlFlow::Exit(2), + VirtualKeyCode::Key3 => *flow = ControlFlow::Exit(3), + VirtualKeyCode::Key4 => *flow = ControlFlow::Exit(4), + VirtualKeyCode::Key5 => *flow = ControlFlow::Exit(5), + VirtualKeyCode::Key6 => *flow = ControlFlow::Exit(6), + VirtualKeyCode::Key7 => *flow = ControlFlow::Exit(7), + VirtualKeyCode::Key8 => *flow = ControlFlow::Exit(8), + VirtualKeyCode::Key9 => *flow = ControlFlow::Exit(9), + VirtualKeyCode::Key0 => *flow = ControlFlow::Exit(0), + _ => (), + }, + Event::WindowEvent { + event: WindowEvent::CloseRequested, + .. + } => *flow = ControlFlow::Exit(47), + _ => (), + } + }); +}