Skip to content

Commit

Permalink
Make keyboard input more controlled
Browse files Browse the repository at this point in the history
  • Loading branch information
hannobraun committed Nov 8, 2024
1 parent f445afa commit 32ec732
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions experiments/2024-10-30/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::sync::Arc;
use std::{collections::BTreeSet, sync::Arc};

use winit::{
application::ApplicationHandler,
event::{KeyEvent, WindowEvent},
event::{ElementState, KeyEvent, WindowEvent},
event_loop::{ActiveEventLoop, EventLoop},
keyboard::{Key, NamedKey},
window::{Window, WindowAttributes, WindowId},
Expand All @@ -19,6 +19,7 @@ pub fn run(ops: OpsLog) -> anyhow::Result<()> {
selected_op,
window: None,
renderer: None,
pressed_keys: BTreeSet::new(),
};
event_loop.run_app(&mut app)?;

Expand All @@ -30,6 +31,7 @@ struct App {
selected_op: usize,
window: Option<Arc<Window>>,
renderer: Option<Renderer>,
pressed_keys: BTreeSet<Key>,
}

impl ApplicationHandler for App {
Expand Down Expand Up @@ -74,9 +76,24 @@ impl ApplicationHandler for App {
event_loop.exit();
}
WindowEvent::KeyboardInput {
event: KeyEvent { logical_key, .. },
event:
KeyEvent {
logical_key, state, ..
},
..
} => {
match state {
ElementState::Pressed => {
if self.pressed_keys.contains(&logical_key) {
return;
}
}
ElementState::Released => {
self.pressed_keys.remove(&logical_key);
return;
}
}

match logical_key {
Key::Named(NamedKey::ArrowDown) => {
if self.selected_op < self.ops.operations.len() {
Expand Down

0 comments on commit 32ec732

Please sign in to comment.