Skip to content

Commit

Permalink
examples: Trigger event-loop exit on Escape key
Browse files Browse the repository at this point in the history
In visual examples it is convenient to just press the escape key to exit
the demo.
  • Loading branch information
MarijnS95 authored and notgull committed Nov 6, 2023
1 parent 636a714 commit ab7688e
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 11 deletions.
14 changes: 12 additions & 2 deletions examples/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
Expand Down
14 changes: 12 additions & 2 deletions examples/fruit.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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();
Expand Down
15 changes: 12 additions & 3 deletions examples/rectangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand All @@ -92,7 +101,7 @@ fn main() {
event:
KeyEvent {
state: ElementState::Pressed,
physical_key: PhysicalKey::Code(KeyCode::Space),
logical_key: Key::Named(NamedKey::Space),
..
},
..
Expand Down
14 changes: 12 additions & 2 deletions examples/winit.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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();
Expand Down
14 changes: 12 additions & 2 deletions examples/winit_wrong_sized_buffer.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit ab7688e

Please sign in to comment.