From 742da95bd7a6f18c1aacc49fb1ce93c537e38576 Mon Sep 17 00:00:00 2001 From: ardocrat Date: Mon, 16 Jun 2025 02:28:27 +0300 Subject: [PATCH] Support for Back button Key on Android (#7073) When your press a Back button on Android (for example at `native-activity`), [Winit translates this key](https://github.com/rust-windowing/winit/blob/47b938dbe78702d521c2c7a43b6f741a3bb8cb0b/src/platform_impl/android/keycodes.rs#L237C42-L237C53) as `NamedKey::BrowserBack`. Added convertion to `Key::Escape` at `egui-winit` module. --------- Co-authored-by: Advocat --- crates/egui-winit/src/lib.rs | 2 ++ crates/egui/src/data/key.rs | 13 ++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/crates/egui-winit/src/lib.rs b/crates/egui-winit/src/lib.rs index c196acaa..f205c310 100644 --- a/crates/egui-winit/src/lib.rs +++ b/crates/egui-winit/src/lib.rs @@ -1144,6 +1144,8 @@ fn key_from_named_key(named_key: winit::keyboard::NamedKey) -> Option NamedKey::F33 => Key::F33, NamedKey::F34 => Key::F34, NamedKey::F35 => Key::F35, + + NamedKey::BrowserBack => Key::BrowserBack, _ => { log::trace!("Unknown key: {named_key:?}"); return None; diff --git a/crates/egui/src/data/key.rs b/crates/egui/src/data/key.rs index 602ec5bd..2a0f33fc 100644 --- a/crates/egui/src/data/key.rs +++ b/crates/egui/src/data/key.rs @@ -183,6 +183,11 @@ pub enum Key { F33, F34, F35, + + /// Back navigation key from multimedia keyboard. + /// Android sends this key on Back button press. + /// Does not work on Web. + BrowserBack, // When adding keys, remember to also update: // * crates/egui-winit/src/lib.rs // * Key::ALL @@ -307,6 +312,8 @@ impl Key { Self::F33, Self::F34, Self::F35, + // Navigation keys: + Self::BrowserBack, ]; /// Converts `"A"` to `Key::A`, `Space` to `Key::Space`, etc. @@ -435,6 +442,8 @@ impl Key { "F34" => Self::F34, "F35" => Self::F35, + "BrowserBack" => Self::BrowserBack, + _ => return None, }) } @@ -588,6 +597,8 @@ impl Key { Self::F33 => "F33", Self::F34 => "F34", Self::F35 => "F35", + + Self::BrowserBack => "BrowserBack", } } } @@ -596,7 +607,7 @@ impl Key { fn test_key_from_name() { assert_eq!( Key::ALL.len(), - Key::F35 as usize + 1, + Key::BrowserBack as usize + 1, "Some keys are missing in Key::ALL" );