Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Objective Allow handling of dead keys on some keyboard layouts. In some cases, dead keys were impossible to get using the `KeyboardInput` event. This information is already present in the underlying winit `KeyEvent`, but it wasn't exposed. ## Solution Expose the `text` field from winit's `KeyEvent` in `KeyboardInput`. This logic is inspired egui's implementation here: https://github.com/emilk/egui/blob/adfc0bebfc6be14cee2068dee758412a5e0648dc/crates/egui-winit/src/lib.rs#L790-L807 ## Testing This is a new field, so it shouldn't break any existing functionality. I tested that this change works by running the modified `text_input` example on different keyboard layouts. ## Example Using a Portuguese/ABNT2 keyboard layout on windows and pressing <kbd>\~</kbd> followed by <kbd>a</kbd>/<kbd>Space</kbd>/<kbd>d</kbd>/<kbd>\~</kbd> now generates the following events: ``` KeyboardInput { key_code: Quote, logical_key: Dead(Some('~')), state: Pressed, text: None, repeat: false, window: 0v1#4294967296 } KeyboardInput { key_code: KeyA, logical_key: Character("ã"), state: Pressed, text: Some("ã"), repeat: false, window: 0v1#4294967296 } KeyboardInput { key_code: Quote, logical_key: Dead(Some('~')), state: Pressed, text: None, repeat: false, window: 0v1#4294967296 } KeyboardInput { key_code: Space, logical_key: Space, state: Pressed, text: Some("~"), repeat: false, window: 0v1#4294967296 } KeyboardInput { key_code: Quote, logical_key: Dead(Some('~')), state: Pressed, text: None, repeat: false, window: 0v1#4294967296 } KeyboardInput { key_code: KeyD, logical_key: Character("d"), state: Pressed, text: Some("~d"), repeat: false, window: 0v1#4294967296 } KeyboardInput { key_code: Quote, logical_key: Dead(Some('~')), state: Pressed, text: None, repeat: false, window: 0v1#4294967296 } KeyboardInput { key_code: Quote, logical_key: Dead(Some('~')), state: Pressed, text: Some("~~"), repeat: false, window: 0v1#4294967296 } ``` The logic for getting an input is pretty simple: check if `text` is `Some`. If it is, this is actual input text, otherwise it isn't. There's a small caveat: certain keys generate control characters in the input text, which needs to be filtered out: ``` KeyboardInput { key_code: Escape, logical_key: Escape, state: Pressed, text: Some("\u{1b}"), repeat: false, window: 0v1#4294967296 } ``` I've updated the text_input example to include egui's solution to this, which works well. ## Migration Guide The `KeyboardInput` event now has a new `text` field.
- Loading branch information