8000 Feature: Extend EguiEvent::Key of it's text representation by obsoleszenz · Pull Request #7105 · emilk/egui · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Feature: Extend EguiEvent::Key of it's text representation #7105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

obsoleszenz
Copy link

This pr adds the text representation of the key to the EguiEvent::Key struct.
This way we get the character that the keyboard layout actually represents and not only
the key combination. For example on EurKey layout, if i press AltRight+A, it's mapped to "ä".
Key { key: A, physical_key: Some(A), pressed: false, repeat: false, modifiers: Modifiers::NONE, text: Some("ä") }

@obsoleszenz obsoleszenz force-pushed the feat_egui_event_key_include_text branch from c4f25d2 to dc46a8a Compare May 31, 2025 13:25
@obsoleszenz obsoleszenz changed the title Extend EguiEvent::Key of it's text representation Feature: Extend EguiEvent::Key of it's text representation May 31, 2025
@obsoleszenz obsoleszenz force-pushed the feat_egui_event_key_include_text branch from dc46a8a to 66f5b26 Compare May 31, 2025 13:52
Copy link
Owner
@emilk emilk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. I need to understand this a bit better though, and a docstring would be a mighty fine way :)

Also: we should implement this on web too!

@@ -436,6 +436,8 @@ pub enum Event {

/// The state of the modifier keys at the time of the event.
modifiers: Modifiers,

text: Option<String>,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a docstring!

How does this compare to Event::Text?
When should this be used instead of Event::Text?
When should Event::Text be used instead of this?
When is it None?
When is it Some("")?

Copy link
Author
@obsoleszenz obsoleszenz Jun 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this compare to Event::Text?

It's the same, but combined. Currently if eframe detects a "text representation" (aka a key combination also represents a character defined by the keyboard layout) eframe emits two events. Once Event::Key for the key combination, once Event::Text with the text representation of it. But this detachment makes it impossible to do custom text input handling. Because one cannot know if a key combination results in a certain "text representation" or not.

When should Event::Text be used instead of this?

If one is only interested in text, but not exactly sure as i don't use this event type

When is it None?

If the key combination doesn't represent a text (more likely a character)

When is it Some("")

Hopefully never :D

Comment on lines -798 to -803
{
// Make sure there is text, and that it is not control characters
// (e.g. delete is sent as "\u{f728}" on macOS).
if !text.is_empty() && text.chars().all(is_printable_char) {
// On some platforms we get here when the user presses Cmd-C (copy), ctrl-W, etc.
// We need to ignore these characters that are side-effects of commands.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of these comment got lost

@obsoleszenz
Copy link
Author

@emilk If you understand this better and approve it to be the way to go i will write a docstring and add the lost comment back :)

@emilk
Copy link
Owner
emilk commented Jun 24, 2025

I think this makes sense, but I still haven't understood a concrete use case for this. If you use KeyEvent::text and ignore Event::Text, then you miss some events (e.g. IME text, clipboard paste text, etc). If you use both, then you get duplicated text.

What is the concrete problem you are trying to solve?

8000

@obsoleszenz
Copy link
Author
obsoleszenz commented Jun 30, 2025

I think this makes sense, but I still haven't understood a concrete use case for this. If you use KeyEvent::text and ignore Event::Text, then you miss some events (e.g. IME text, clipboard paste text, etc). If you use both, then you get duplicated text.

What is the concrete problem you are trying to solve?

I give you an example of what i currently do:
I'm using egui mainly for painting, not using the input widgets. But i have my own implementations of it, as i focus on having the whole app controllable with the keyboard (think game).
So for example i have a popup which allows editing some text, one can open it by pressing Esc, followed by e. If the user now presses any keys it will edit/insert the text, escape closes the popup, enter saves. But I want to support ä/ü... which is mapped on my keyboard to Alt+a & Alt+u.

I get following events:

  • Egui::Event::Key { ... Escape } => Open main menu popup
  • Egui::Event::Key { ... 'e' } => switch to edit text popup
  • Egui::Event::Text("e") => We now insert text "e" and there's no way to know that we processed this event as Event::Key already
  • Egui::Event::Key(Alt(a)) => We don't know that this represents "ä", so we ignore
  • Egui::Event::Text("ä") => Insert "ä"

@emilk
Copy link
Owner
emilk commented Jul 7, 2025

Thanks for explaining this in more detail! It seems the core need is to be able to match a specific Key event to a specific Text event, and the other way around. The proposed solution is one way of doing so, but I'm not sure it's the best way.

Or rather, maybe it doesn't go far enough 🤔 If we add Option<String> to Event::Key, we should also remove Event::Text. I earlier misremembered and thought we used Event::Text for non-key events too, like paste and IME, but those have their own events. So I think Event::Text is safe to be removed (or rather moved into Event::Key).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0