examples: Trigger event-loop exit on Escape key

In visual examples it is convenient to just press the escape key to exit
the demo.
This commit is contained in:
Marijn Suijten 2023-11-06 09:34:49 +01:00 committed by John Nunley
parent 636a7148c8
commit ab7688e2ed
5 changed files with 60 additions and 11 deletions

View file

@ -4,8 +4,9 @@ use rayon::prelude::*;
use std::f64::consts::PI;
use std::num::NonZeroU32;
use std::rc::Rc;
use winit::event::{Event, WindowEvent};
use winit::event::{Event, KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::keyboard::{Key, NamedKey};
use winit::window::WindowBuilder;
fn main() {
@ -65,7 +66,16 @@ fn main() {
window.request_redraw();
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
event:
WindowEvent::CloseRequested
| WindowEvent::KeyboardInput {
event:
KeyEvent {
logical_key: Key::Named(NamedKey::Escape),
..
},
..
},
window_id,
} if window_id == window.id() => {
elwt.exit();

View file

@ -1,8 +1,9 @@
use image::GenericImageView;
use std::num::NonZeroU32;
use std::rc::Rc;
use winit::event::{Event, WindowEvent};
use winit::event::{Event, KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::keyboard::{Key, NamedKey};
use winit::window::WindowBuilder;
fn main() {
@ -64,7 +65,16 @@ fn main() {
buffer.present().unwrap();
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
event:
WindowEvent::CloseRequested
| WindowEvent::KeyboardInput {
event:
KeyEvent {
logical_key: Key::Named(NamedKey::Escape),
..
},
..
},
window_id,
} if window_id == window.id() => {
elwt.exit();

View file

@ -2,7 +2,7 @@ use std::num::NonZeroU32;
use std::rc::Rc;
use winit::event::{ElementState, Event, KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::keyboard::{KeyCode, PhysicalKey};
use winit::keyboard::{Key, NamedKey};
use winit::window::WindowBuilder;
fn redraw(buffer: &mut [u32], width: usize, height: usize, flag: bool) {
@ -80,7 +80,16 @@ fn main() {
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
event:
WindowEvent::CloseRequested
| WindowEvent::KeyboardInput {
event:
KeyEvent {
logical_key: Key::Named(NamedKey::Escape),
..
},
..
},
window_id,
} if window_id == window.id() => {
elwt.exit();
@ -92,7 +101,7 @@ fn main() {
event:
KeyEvent {
state: ElementState::Pressed,
physical_key: PhysicalKey::Code(KeyCode::Space),
logical_key: Key::Named(NamedKey::Space),
..
},
..

View file

@ -1,7 +1,8 @@
use std::num::NonZeroU32;
use std::rc::Rc;
use winit::event::{Event, WindowEvent};
use winit::event::{Event, KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::keyboard::{Key, NamedKey};
use winit::window::WindowBuilder;
fn main() {
@ -55,7 +56,16 @@ fn main() {
}
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
event:
WindowEvent::CloseRequested
| WindowEvent::KeyboardInput {
event:
KeyEvent {
logical_key: Key::Named(NamedKey::Escape),
..
},
..
},
window_id,
} if window_id == window.id() => {
elwt.exit();

View file

@ -1,7 +1,8 @@
use std::num::NonZeroU32;
use std::rc::Rc;
use winit::event::{Event, WindowEvent};
use winit::event::{Event, KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::keyboard::{Key, NamedKey};
use winit::window::WindowBuilder;
const BUFFER_WIDTH: usize = 256;
@ -58,7 +59,16 @@ fn main() {
buffer.present().unwrap();
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
event:
WindowEvent::CloseRequested
| WindowEvent::KeyboardInput {
event:
KeyEvent {
logical_key: Key::Named(NamedKey::Escape),
..
},
..
},
window_id,
} if window_id == window.id() => {
elwt.exit();