Add an input event history tracker to the egui demo lib (#4693)

This is great for testing how e.g. keyboard events are seen by egui:


![image](https://github.com/emilk/egui/assets/1148717/b2187060-6533-439c-9f43-fc49b8213c28)


* Relevant: https://github.com/emilk/egui/issues/3653
This commit is contained in:
Emil Ernerfeldt
2024-06-23 11:34:38 +02:00
committed by GitHub
parent fb4c6cc619
commit 44f49713eb
6 changed files with 193 additions and 17 deletions
+35 -1
View File
@@ -544,7 +544,7 @@ pub const NUM_POINTER_BUTTONS: usize = 5;
/// NOTE: For cross-platform uses, ALT+SHIFT is a bad combination of modifiers
/// as on mac that is how you type special characters,
/// so those key presses are usually not reported to egui.
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq)]
#[derive(Clone, Copy, Default, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Modifiers {
/// Either of the alt keys are down (option ⌥ on Mac).
@@ -567,6 +567,40 @@ pub struct Modifiers {
pub command: bool,
}
impl std::fmt::Debug for Modifiers {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.is_none() {
return write!(f, "Modifiers::NONE");
}
let Self {
alt,
ctrl,
shift,
mac_cmd,
command,
} = *self;
let mut debug = f.debug_struct("Modifiers");
if alt {
debug.field("alt", &true);
}
if ctrl {
debug.field("ctrl", &true);
}
if shift {
debug.field("shift", &true);
}
if mac_cmd {
debug.field("mac_cmd", &true);
}
if command {
debug.field("command", &true);
}
debug.finish()
}
}
impl Modifiers {
pub const NONE: Self = Self {
alt: false,
+21 -14
View File
@@ -1,10 +1,12 @@
/// Keyboard keys.
///
/// egui usually uses logical keys, i.e. after applying any user keymap.
// TODO(emilk): split into `LogicalKey` and `PhysicalKey`
/// egui usually uses logical keys, i.e. after applying any user keymap.\
// See comment at the end of `Key { … }` on how to add new keys.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum Key {
// ----------------------------------------------
// Commands:
ArrowDown,
ArrowLeft,
ArrowRight,
@@ -73,34 +75,34 @@ pub enum Key {
// ----------------------------------------------
// Digits:
/// Either from the main row or from the numpad.
/// `0` (from main row or numpad)
Num0,
/// Either from the main row or from the numpad.
/// `1` (from main row or numpad)
Num1,
/// Either from the main row or from the numpad.
/// `2` (from main row or numpad)
Num2,
/// Either from the main row or from the numpad.
/// `3` (from main row or numpad)
Num3,
/// Either from the main row or from the numpad.
/// `4` (from main row or numpad)
Num4,
/// Either from the main row or from the numpad.
/// `5` (from main row or numpad)
Num5,
/// Either from the main row or from the numpad.
/// `6` (from main row or numpad)
Num6,
/// Either from the main row or from the numpad.
/// `7` (from main row or numpad)
Num7,
/// Either from the main row or from the numpad.
/// `8` (from main row or numpad)
Num8,
/// Either from the main row or from the numpad.
/// `9` (from main row or numpad)
Num9,
// ----------------------------------------------
@@ -169,14 +171,19 @@ pub enum Key {
F33,
F34,
F35,
// When adding keys, remember to also update `crates/egui-winit/src/lib.rs`
// and [`Self::ALL`].
// When adding keys, remember to also update:
// * crates/egui-winit/src/lib.rs
// * Key::ALL
// * Key::from_name
// You should test that it works using the "Input Event History" window in the egui demo app.
// Make sure to test both natively and on web!
// Also: don't add keys last; add them to the group they best belong to.
}
impl Key {
/// All egui keys
pub const ALL: &'static [Self] = &[
// Commands:
Self::ArrowDown,
Self::ArrowLeft,
Self::ArrowRight,