From 90797f04f4af00487b8baca4e493349b0c13db21 Mon Sep 17 00:00:00 2001 From: Michael Tang Date: Tue, 12 Jan 2021 04:46:27 -0800 Subject: [PATCH] Implement InputState::key_down (#107) --- egui/src/input.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/egui/src/input.rs b/egui/src/input.rs index ba85ec12..b72e99e8 100644 --- a/egui/src/input.rs +++ b/egui/src/input.rs @@ -1,5 +1,7 @@ //! The input needed by Egui. +use std::collections::HashSet; + use crate::{math::*, util::History}; /// If mouse moves more than this, it is no longer a click (but maybe a drag) @@ -125,6 +127,9 @@ pub struct InputState { /// Which modifier keys are down at the start of the frame? pub modifiers: Modifiers, + // The keys that are currently being held down. + pub keys_down: HashSet, + /// In-order events received this frame pub events: Vec, } @@ -141,6 +146,7 @@ impl Default for InputState { unstable_dt: 1.0 / 6.0, predicted_dt: 1.0 / 6.0, modifiers: Default::default(), + keys_down: Default::default(), events: Default::default(), } } @@ -258,7 +264,7 @@ pub struct Modifiers { /// /// Many keys are omitted because they are not always physical keys (depending on keyboard language), e.g. `;` and `ยง`, /// and are therefor unsuitable as keyboard shortcuts if you want your app to be portable. -#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)] +#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)] pub enum Key { ArrowDown, ArrowLeft, @@ -344,6 +350,16 @@ impl InputState { } }); let mouse = self.mouse.begin_frame(time, &new); + let mut keys_down = self.keys_down; + for event in &new.events { + if let Event::Key { key, pressed, .. } = event { + if *pressed { + keys_down.insert(*key); + } else { + keys_down.remove(key); + } + } + } InputState { mouse, scroll_delta: new.scroll_delta, @@ -353,6 +369,7 @@ impl InputState { unstable_dt, predicted_dt: new.predicted_dt, modifiers: new.modifiers, + keys_down, events: new.events.clone(), // TODO: remove clone() and use raw.events raw: new, } @@ -384,6 +401,11 @@ impl InputState { }) } + /// Is the given key currently held down? + pub fn key_down(&self, desired_key: Key) -> bool { + self.keys_down.contains(&desired_key) + } + /// Was the given key released this frame? pub fn key_released(&self, desired_key: Key) -> bool { self.events.iter().any(|event| { @@ -536,6 +558,7 @@ impl InputState { unstable_dt, predicted_dt, modifiers, + keys_down, events, } = self; @@ -561,6 +584,7 @@ impl InputState { )); ui.label(format!("predicted_dt: {:.1} ms", 1e3 * predicted_dt)); ui.label(format!("modifiers: {:#?}", modifiers)); + ui.label(format!("keys_down: {:?}", keys_down)); ui.label(format!("events: {:?}", events)) .on_hover_text("key presses etc"); }