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:
parent
636a7148c8
commit
ab7688e2ed
5 changed files with 60 additions and 11 deletions
|
|
@ -4,8 +4,9 @@ use rayon::prelude::*;
|
||||||
use std::f64::consts::PI;
|
use std::f64::consts::PI;
|
||||||
use std::num::NonZeroU32;
|
use std::num::NonZeroU32;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use winit::event::{Event, WindowEvent};
|
use winit::event::{Event, KeyEvent, WindowEvent};
|
||||||
use winit::event_loop::{ControlFlow, EventLoop};
|
use winit::event_loop::{ControlFlow, EventLoop};
|
||||||
|
use winit::keyboard::{Key, NamedKey};
|
||||||
use winit::window::WindowBuilder;
|
use winit::window::WindowBuilder;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
@ -65,7 +66,16 @@ fn main() {
|
||||||
window.request_redraw();
|
window.request_redraw();
|
||||||
}
|
}
|
||||||
Event::WindowEvent {
|
Event::WindowEvent {
|
||||||
event: WindowEvent::CloseRequested,
|
event:
|
||||||
|
WindowEvent::CloseRequested
|
||||||
|
| WindowEvent::KeyboardInput {
|
||||||
|
event:
|
||||||
|
KeyEvent {
|
||||||
|
logical_key: Key::Named(NamedKey::Escape),
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
},
|
||||||
window_id,
|
window_id,
|
||||||
} if window_id == window.id() => {
|
} if window_id == window.id() => {
|
||||||
elwt.exit();
|
elwt.exit();
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
use image::GenericImageView;
|
use image::GenericImageView;
|
||||||
use std::num::NonZeroU32;
|
use std::num::NonZeroU32;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use winit::event::{Event, WindowEvent};
|
use winit::event::{Event, KeyEvent, WindowEvent};
|
||||||
use winit::event_loop::{ControlFlow, EventLoop};
|
use winit::event_loop::{ControlFlow, EventLoop};
|
||||||
|
use winit::keyboard::{Key, NamedKey};
|
||||||
use winit::window::WindowBuilder;
|
use winit::window::WindowBuilder;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
@ -64,7 +65,16 @@ fn main() {
|
||||||
buffer.present().unwrap();
|
buffer.present().unwrap();
|
||||||
}
|
}
|
||||||
Event::WindowEvent {
|
Event::WindowEvent {
|
||||||
event: WindowEvent::CloseRequested,
|
event:
|
||||||
|
WindowEvent::CloseRequested
|
||||||
|
| WindowEvent::KeyboardInput {
|
||||||
|
event:
|
||||||
|
KeyEvent {
|
||||||
|
logical_key: Key::Named(NamedKey::Escape),
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
},
|
||||||
window_id,
|
window_id,
|
||||||
} if window_id == window.id() => {
|
} if window_id == window.id() => {
|
||||||
elwt.exit();
|
elwt.exit();
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use std::num::NonZeroU32;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use winit::event::{ElementState, Event, KeyEvent, WindowEvent};
|
use winit::event::{ElementState, Event, KeyEvent, WindowEvent};
|
||||||
use winit::event_loop::{ControlFlow, EventLoop};
|
use winit::event_loop::{ControlFlow, EventLoop};
|
||||||
use winit::keyboard::{KeyCode, PhysicalKey};
|
use winit::keyboard::{Key, NamedKey};
|
||||||
use winit::window::WindowBuilder;
|
use winit::window::WindowBuilder;
|
||||||
|
|
||||||
fn redraw(buffer: &mut [u32], width: usize, height: usize, flag: bool) {
|
fn redraw(buffer: &mut [u32], width: usize, height: usize, flag: bool) {
|
||||||
|
|
@ -80,7 +80,16 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
Event::WindowEvent {
|
Event::WindowEvent {
|
||||||
event: WindowEvent::CloseRequested,
|
event:
|
||||||
|
WindowEvent::CloseRequested
|
||||||
|
| WindowEvent::KeyboardInput {
|
||||||
|
event:
|
||||||
|
KeyEvent {
|
||||||
|
logical_key: Key::Named(NamedKey::Escape),
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
},
|
||||||
window_id,
|
window_id,
|
||||||
} if window_id == window.id() => {
|
} if window_id == window.id() => {
|
||||||
elwt.exit();
|
elwt.exit();
|
||||||
|
|
@ -92,7 +101,7 @@ fn main() {
|
||||||
event:
|
event:
|
||||||
KeyEvent {
|
KeyEvent {
|
||||||
state: ElementState::Pressed,
|
state: ElementState::Pressed,
|
||||||
physical_key: PhysicalKey::Code(KeyCode::Space),
|
logical_key: Key::Named(NamedKey::Space),
|
||||||
..
|
..
|
||||||
},
|
},
|
||||||
..
|
..
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
use std::num::NonZeroU32;
|
use std::num::NonZeroU32;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use winit::event::{Event, WindowEvent};
|
use winit::event::{Event, KeyEvent, WindowEvent};
|
||||||
use winit::event_loop::{ControlFlow, EventLoop};
|
use winit::event_loop::{ControlFlow, EventLoop};
|
||||||
|
use winit::keyboard::{Key, NamedKey};
|
||||||
use winit::window::WindowBuilder;
|
use winit::window::WindowBuilder;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
@ -55,7 +56,16 @@ fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::WindowEvent {
|
Event::WindowEvent {
|
||||||
event: WindowEvent::CloseRequested,
|
event:
|
||||||
|
WindowEvent::CloseRequested
|
||||||
|
| WindowEvent::KeyboardInput {
|
||||||
|
event:
|
||||||
|
KeyEvent {
|
||||||
|
logical_key: Key::Named(NamedKey::Escape),
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
},
|
||||||
window_id,
|
window_id,
|
||||||
} if window_id == window.id() => {
|
} if window_id == window.id() => {
|
||||||
elwt.exit();
|
elwt.exit();
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
use std::num::NonZeroU32;
|
use std::num::NonZeroU32;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use winit::event::{Event, WindowEvent};
|
use winit::event::{Event, KeyEvent, WindowEvent};
|
||||||
use winit::event_loop::{ControlFlow, EventLoop};
|
use winit::event_loop::{ControlFlow, EventLoop};
|
||||||
|
use winit::keyboard::{Key, NamedKey};
|
||||||
use winit::window::WindowBuilder;
|
use winit::window::WindowBuilder;
|
||||||
|
|
||||||
const BUFFER_WIDTH: usize = 256;
|
const BUFFER_WIDTH: usize = 256;
|
||||||
|
|
@ -58,7 +59,16 @@ fn main() {
|
||||||
buffer.present().unwrap();
|
buffer.present().unwrap();
|
||||||
}
|
}
|
||||||
Event::WindowEvent {
|
Event::WindowEvent {
|
||||||
event: WindowEvent::CloseRequested,
|
event:
|
||||||
|
WindowEvent::CloseRequested
|
||||||
|
| WindowEvent::KeyboardInput {
|
||||||
|
event:
|
||||||
|
KeyEvent {
|
||||||
|
logical_key: Key::Named(NamedKey::Escape),
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
},
|
||||||
window_id,
|
window_id,
|
||||||
} if window_id == window.id() => {
|
} if window_id == window.id() => {
|
||||||
elwt.exit();
|
elwt.exit();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue