Skip to content

Commit a9baf5e

Browse files
committed
fix(android): Populate KeyEvent.text via Key::to_text()
The `text` field on `KeyEvent` was hardcoded to `None` on Android, making it impossible for custom `NativeActivity` subclasses that show the IME to receive functional text input using *for example* the existing `winit-egui` crate which relies on this field being set. Use `Key::to_text()` on press events to derive `text` from `logical_key`, matching the convention used by the Windows and macOS backends. Supposedly that doesn't include all kinds of special virtual unicode keys, but at least the basics work this way.
1 parent 6bb43fd commit a9baf5e

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

src/changelog/unreleased.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,4 @@ changelog entry.
5151
- On X11, fix debug mode overflow panic in `set_timestamp`.
5252
- On macOS, fix crash in `set_marked_text` when native Pinyin IME sends out-of-bounds `selected_range`.
5353
- On Windows, fix `WM_IME_SETCONTEXT` IME UI flag masking on `lParam`.
54+
- On Android, populate `KeyEvent::text` and `KeyEvent::text_with_all_modifiers` via `Key::to_text()`

src/platform_impl/android/mod.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,17 +446,24 @@ impl<T: 'static> EventLoop<T> {
446446
&mut self.combining_accent,
447447
);
448448

449+
let logical_key = keycodes::to_logical(key_char, keycode);
450+
let text = if state == event::ElementState::Pressed {
451+
logical_key.to_text().map(smol_str::SmolStr::new)
452+
} else {
453+
None
454+
};
455+
449456
let event = event::Event::WindowEvent {
450457
window_id: window::WindowId(WindowId),
451458
event: event::WindowEvent::KeyboardInput {
452459
device_id: event::DeviceId(DeviceId(key.device_id())),
453460
event: event::KeyEvent {
454461
state,
455462
physical_key: keycodes::to_physical_key(keycode),
456-
logical_key: keycodes::to_logical(key_char, keycode),
463+
logical_key,
457464
location: keycodes::to_location(keycode),
458465
repeat: key.repeat_count() > 0,
459-
text: None,
466+
text,
460467
platform_specific: KeyEventExtra {},
461468
},
462469
is_synthetic: false,

0 commit comments

Comments
 (0)