# 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:
adfc0bebfc/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.
		
	
			
		
			
				
	
	
		
			186 lines
		
	
	
		
			5.6 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			186 lines
		
	
	
		
			5.6 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
//! Simple text input support
 | 
						|
//!
 | 
						|
//! Return creates a new line, backspace removes the last character.
 | 
						|
//! Clicking toggle IME (Input Method Editor) support, but the font used as limited support of characters.
 | 
						|
//! You should change the provided font with another one to test other languages input.
 | 
						|
 | 
						|
use std::mem;
 | 
						|
 | 
						|
use bevy::{
 | 
						|
    input::keyboard::{Key, KeyboardInput},
 | 
						|
    prelude::*,
 | 
						|
};
 | 
						|
 | 
						|
fn main() {
 | 
						|
    App::new()
 | 
						|
        .add_plugins(DefaultPlugins)
 | 
						|
        .add_systems(Startup, setup_scene)
 | 
						|
        .add_systems(
 | 
						|
            Update,
 | 
						|
            (
 | 
						|
                toggle_ime,
 | 
						|
                listen_ime_events,
 | 
						|
                listen_keyboard_input_events,
 | 
						|
                bubbling_text,
 | 
						|
            ),
 | 
						|
        )
 | 
						|
        .run();
 | 
						|
}
 | 
						|
 | 
						|
fn setup_scene(mut commands: Commands, asset_server: Res<AssetServer>) {
 | 
						|
    commands.spawn(Camera2d);
 | 
						|
 | 
						|
    // The default font has a limited number of glyphs, so use the full version for
 | 
						|
    // sections that will hold text input.
 | 
						|
    let font = asset_server.load("fonts/FiraMono-Medium.ttf");
 | 
						|
 | 
						|
    commands
 | 
						|
        .spawn((
 | 
						|
            Text::default(),
 | 
						|
            Node {
 | 
						|
                position_type: PositionType::Absolute,
 | 
						|
                top: Val::Px(12.0),
 | 
						|
                left: Val::Px(12.0),
 | 
						|
                ..default()
 | 
						|
            },
 | 
						|
        ))
 | 
						|
        .with_children(|p| {
 | 
						|
            p.spawn(TextSpan::new(
 | 
						|
                "Click to toggle IME. Press return to start a new line.\n\n",
 | 
						|
            ));
 | 
						|
            p.spawn(TextSpan::new("IME Enabled: "));
 | 
						|
            p.spawn(TextSpan::new("false\n"));
 | 
						|
            p.spawn(TextSpan::new("IME Active:  "));
 | 
						|
            p.spawn(TextSpan::new("false\n"));
 | 
						|
            p.spawn(TextSpan::new("IME Buffer:  "));
 | 
						|
            p.spawn((
 | 
						|
                TextSpan::new("\n"),
 | 
						|
                TextFont {
 | 
						|
                    font: font.clone(),
 | 
						|
                    ..default()
 | 
						|
                },
 | 
						|
            ));
 | 
						|
        });
 | 
						|
 | 
						|
    commands.spawn((
 | 
						|
        Text2d::new(""),
 | 
						|
        TextFont {
 | 
						|
            font,
 | 
						|
            font_size: 100.0,
 | 
						|
            ..default()
 | 
						|
        },
 | 
						|
    ));
 | 
						|
}
 | 
						|
 | 
						|
fn toggle_ime(
 | 
						|
    input: Res<ButtonInput<MouseButton>>,
 | 
						|
    mut window: Single<&mut Window>,
 | 
						|
    status_text: Single<Entity, (With<Node>, With<Text>)>,
 | 
						|
    mut ui_writer: TextUiWriter,
 | 
						|
) {
 | 
						|
    if input.just_pressed(MouseButton::Left) {
 | 
						|
        window.ime_position = window.cursor_position().unwrap();
 | 
						|
        window.ime_enabled = !window.ime_enabled;
 | 
						|
 | 
						|
        *ui_writer.text(*status_text, 3) = format!("{}\n", window.ime_enabled);
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
#[derive(Component)]
 | 
						|
struct Bubble {
 | 
						|
    timer: Timer,
 | 
						|
}
 | 
						|
 | 
						|
fn bubbling_text(
 | 
						|
    mut commands: Commands,
 | 
						|
    mut bubbles: Query<(Entity, &mut Transform, &mut Bubble)>,
 | 
						|
    time: Res<Time>,
 | 
						|
) {
 | 
						|
    for (entity, mut transform, mut bubble) in bubbles.iter_mut() {
 | 
						|
        if bubble.timer.tick(time.delta()).just_finished() {
 | 
						|
            commands.entity(entity).despawn();
 | 
						|
        }
 | 
						|
        transform.translation.y += time.delta_secs() * 100.0;
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
fn listen_ime_events(
 | 
						|
    mut events: EventReader<Ime>,
 | 
						|
    status_text: Single<Entity, (With<Node>, With<Text>)>,
 | 
						|
    mut edit_text: Single<&mut Text2d, (Without<Node>, Without<Bubble>)>,
 | 
						|
    mut ui_writer: TextUiWriter,
 | 
						|
) {
 | 
						|
    for event in events.read() {
 | 
						|
        match event {
 | 
						|
            Ime::Preedit { value, cursor, .. } if !cursor.is_none() => {
 | 
						|
                *ui_writer.text(*status_text, 7) = format!("{value}\n");
 | 
						|
            }
 | 
						|
            Ime::Preedit { cursor, .. } if cursor.is_none() => {
 | 
						|
                *ui_writer.text(*status_text, 7) = "\n".to_string();
 | 
						|
            }
 | 
						|
            Ime::Commit { value, .. } => {
 | 
						|
                edit_text.push_str(value);
 | 
						|
            }
 | 
						|
            Ime::Enabled { .. } => {
 | 
						|
                *ui_writer.text(*status_text, 5) = "true\n".to_string();
 | 
						|
            }
 | 
						|
            Ime::Disabled { .. } => {
 | 
						|
                *ui_writer.text(*status_text, 5) = "false\n".to_string();
 | 
						|
            }
 | 
						|
            _ => (),
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
fn listen_keyboard_input_events(
 | 
						|
    mut commands: Commands,
 | 
						|
    mut events: EventReader<KeyboardInput>,
 | 
						|
    edit_text: Single<(&mut Text2d, &TextFont), (Without<Node>, Without<Bubble>)>,
 | 
						|
) {
 | 
						|
    let (mut text, style) = edit_text.into_inner();
 | 
						|
    for event in events.read() {
 | 
						|
        // Only trigger changes when the key is first pressed.
 | 
						|
        if !event.state.is_pressed() {
 | 
						|
            continue;
 | 
						|
        }
 | 
						|
 | 
						|
        match (&event.logical_key, &event.text) {
 | 
						|
            (Key::Enter, _) => {
 | 
						|
                if text.is_empty() {
 | 
						|
                    continue;
 | 
						|
                }
 | 
						|
                let old_value = mem::take(&mut **text);
 | 
						|
 | 
						|
                commands.spawn((
 | 
						|
                    Text2d::new(old_value),
 | 
						|
                    style.clone(),
 | 
						|
                    Bubble {
 | 
						|
                        timer: Timer::from_seconds(5.0, TimerMode::Once),
 | 
						|
                    },
 | 
						|
                ));
 | 
						|
            }
 | 
						|
            (Key::Backspace, _) => {
 | 
						|
                text.pop();
 | 
						|
            }
 | 
						|
            (_, Some(inserted_text)) => {
 | 
						|
                // Make sure the text doesn't have any control characters,
 | 
						|
                // which can happen when keys like Escape are pressed
 | 
						|
                if inserted_text.chars().all(is_printable_char) {
 | 
						|
                    text.push_str(inserted_text);
 | 
						|
                }
 | 
						|
            }
 | 
						|
            _ => continue,
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
// this logic is taken from egui-winit:
 | 
						|
// https://github.com/emilk/egui/blob/adfc0bebfc6be14cee2068dee758412a5e0648dc/crates/egui-winit/src/lib.rs#L1014-L1024
 | 
						|
fn is_printable_char(chr: char) -> bool {
 | 
						|
    let is_in_private_use_area = ('\u{e000}'..='\u{f8ff}').contains(&chr)
 | 
						|
        || ('\u{f0000}'..='\u{ffffd}').contains(&chr)
 | 
						|
        || ('\u{100000}'..='\u{10fffd}').contains(&chr);
 | 
						|
 | 
						|
    !is_in_private_use_area && !chr.is_ascii_control()
 | 
						|
}
 |