mirror of
https://github.com/italicsjenga/winit-sonoma-fix.git
synced 2024-12-23 13:51:30 +11:00
918430979f
Overhaul the keyboard API in winit to mimic the W3C specification to achieve better crossplatform parity. The `KeyboardInput` event is now uses `KeyEvent` which consists of: - `physical_key` - a cross platform way to refer to scancodes; - `logical_key` - keysym value, which shows your key respecting the layout; - `text` - the text produced by this keypress; - `location` - the location of the key on the keyboard; - `repeat` - whether the key was produced by the repeat. And also a `platform_specific` field which encapsulates extra information on desktop platforms, like key without modifiers and text with all modifiers. The `Modifiers` were also slightly reworked as in, the information whether the left or right modifier is pressed is now also exposed on platforms where it could be queried reliably. The support was also added for the web and orbital platforms finishing the API change. This change made the `OptionAsAlt` API on macOS redundant thus it was removed all together. Co-authored-by: Artúr Kovács <kovacs.artur.barnabas@gmail.com> Co-authored-by: Kirill Chibisov <contact@kchibisov.com> Co-authored-by: daxpedda <daxpedda@gmail.com> Fixes: #2631. Fixes: #2055. Fixes: #2032. Fixes: #1904. Fixes: #1810. Fixes: #1700. Fixes: #1443. Fixes: #1343. Fixes: #1208. Fixes: #1151. Fixes: #812. Fixes: #600. Fixes: #361. Fixes: #343.
118 lines
4 KiB
Rust
118 lines
4 KiB
Rust
#![allow(clippy::single_match)]
|
|
|
|
use std::{thread, time};
|
|
|
|
use simple_logger::SimpleLogger;
|
|
use winit::{
|
|
event::{ElementState, Event, KeyEvent, WindowEvent},
|
|
event_loop::EventLoop,
|
|
keyboard::Key,
|
|
window::WindowBuilder,
|
|
};
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
enum Mode {
|
|
Wait,
|
|
WaitUntil,
|
|
Poll,
|
|
}
|
|
|
|
const WAIT_TIME: time::Duration = time::Duration::from_millis(100);
|
|
const POLL_SLEEP_TIME: time::Duration = time::Duration::from_millis(100);
|
|
|
|
fn main() {
|
|
SimpleLogger::new().init().unwrap();
|
|
|
|
println!("Press '1' to switch to Wait mode.");
|
|
println!("Press '2' to switch to WaitUntil mode.");
|
|
println!("Press '3' to switch to Poll mode.");
|
|
println!("Press 'R' to toggle request_redraw() calls.");
|
|
println!("Press 'Esc' to close the window.");
|
|
|
|
let event_loop = EventLoop::new();
|
|
let window = WindowBuilder::new()
|
|
.with_title("Press 1, 2, 3 to change control flow mode. Press R to toggle redraw requests.")
|
|
.build(&event_loop)
|
|
.unwrap();
|
|
|
|
let mut mode = Mode::Wait;
|
|
let mut request_redraw = false;
|
|
let mut wait_cancelled = false;
|
|
let mut close_requested = false;
|
|
|
|
event_loop.run(move |event, _, control_flow| {
|
|
use winit::event::StartCause;
|
|
println!("{event:?}");
|
|
match event {
|
|
Event::NewEvents(start_cause) => {
|
|
wait_cancelled = match start_cause {
|
|
StartCause::WaitCancelled { .. } => mode == Mode::WaitUntil,
|
|
_ => false,
|
|
}
|
|
}
|
|
Event::WindowEvent { event, .. } => match event {
|
|
WindowEvent::CloseRequested => {
|
|
close_requested = true;
|
|
}
|
|
WindowEvent::KeyboardInput {
|
|
event:
|
|
KeyEvent {
|
|
logical_key: key,
|
|
state: ElementState::Pressed,
|
|
..
|
|
},
|
|
..
|
|
} => match key.as_ref() {
|
|
// WARNING: Consider using `key_without_modifers()` if available on your platform.
|
|
// See the `key_binding` example
|
|
Key::Character("1") => {
|
|
mode = Mode::Wait;
|
|
println!("\nmode: {mode:?}\n");
|
|
}
|
|
Key::Character("2") => {
|
|
mode = Mode::WaitUntil;
|
|
println!("\nmode: {mode:?}\n");
|
|
}
|
|
Key::Character("3") => {
|
|
mode = Mode::Poll;
|
|
println!("\nmode: {mode:?}\n");
|
|
}
|
|
Key::Character("r") => {
|
|
request_redraw = !request_redraw;
|
|
println!("\nrequest_redraw: {request_redraw}\n");
|
|
}
|
|
Key::Escape => {
|
|
close_requested = true;
|
|
}
|
|
_ => (),
|
|
},
|
|
_ => (),
|
|
},
|
|
Event::MainEventsCleared => {
|
|
if request_redraw && !wait_cancelled && !close_requested {
|
|
window.request_redraw();
|
|
}
|
|
if close_requested {
|
|
control_flow.set_exit();
|
|
}
|
|
}
|
|
Event::RedrawRequested(_window_id) => {}
|
|
Event::RedrawEventsCleared => {
|
|
match mode {
|
|
Mode::Wait => control_flow.set_wait(),
|
|
Mode::WaitUntil => {
|
|
if !wait_cancelled {
|
|
control_flow.set_wait_until(instant::Instant::now() + WAIT_TIME);
|
|
}
|
|
}
|
|
Mode::Poll => {
|
|
thread::sleep(POLL_SLEEP_TIME);
|
|
control_flow.set_poll();
|
|
}
|
|
};
|
|
}
|
|
_ => (),
|
|
}
|
|
});
|
|
}
|