Fix text input on Android (#5759)

<!--
Please read the "Making a PR" section of
[`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/master/CONTRIBUTING.md)
before opening a Pull Request!

* Keep your PR:s small and focused.
* The PR title is what ends up in the changelog, so make it descriptive!
* If applicable, add a screenshot or gif.
* If it is a non-trivial addition, consider adding a demo for it to
`egui_demo_lib`, or a new example.
* Do NOT open PR:s from your `master` branch, as that makes it hard for
maintainers to test and add commits to your PR.
* Remember to run `cargo fmt` and `cargo clippy`.
* Open the PR as a draft until you have self-reviewed it and run
`./scripts/check.sh`.
* When you have addressed a PR comment, mark it as resolved.

Please be patient! I will review your PR, but my time is limited!
-->

* [x] I have followed the instructions in the PR template

This fixes an issue on android where keyboard input is not registered in
text boxes because `winit` does not fill in the `text` field of the
`KeyEvent`

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
StratusFearMe21 2025-03-21 07:35:46 -06:00 committed by GitHub
parent 91f02f9e87
commit 390e0bfc1e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 8 additions and 4 deletions

View File

@ -729,7 +729,7 @@ impl State {
// When telling users "Press Ctrl-F to find", this is where we should
// look for the "F" key, because they may have a dvorak layout on
// a qwerty keyboard, and so the logical "F" character may not be located on the physical `KeyCode::KeyF` position.
logical_key,
logical_key: winit_logical_key,
text,
@ -748,7 +748,7 @@ impl State {
None
};
let logical_key = key_from_winit_key(logical_key);
let logical_key = key_from_winit_key(winit_logical_key);
// Helpful logging to enable when adding new key support
log::trace!(
@ -791,7 +791,11 @@ impl State {
});
}
if let Some(text) = &text {
if let Some(text) = text
.as_ref()
.map(|t| t.as_str())
.or_else(|| winit_logical_key.to_text())
{
// 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) {
@ -805,7 +809,7 @@ impl State {
if pressed && !is_cmd {
self.egui_input
.events
.push(egui::Event::Text(text.to_string()));
.push(egui::Event::Text(text.to_owned()));
}
}
}