virtual_keyboard/
virtual_keyboard.rs

1//! Virtual keyboard example
2
3use bevy::{
4    color::palettes::css::NAVY,
5    feathers::{
6        controls::{virtual_keyboard, VirtualKeyPressed},
7        dark_theme::create_dark_theme,
8        theme::UiTheme,
9        FeathersPlugins,
10    },
11    prelude::*,
12    ui_widgets::observe,
13};
14
15fn main() {
16    App::new()
17        .add_plugins((DefaultPlugins, FeathersPlugins))
18        .insert_resource(UiTheme(create_dark_theme()))
19        .add_systems(Startup, setup)
20        .run();
21}
22
23fn on_virtual_key_pressed(virtual_key_pressed: On<VirtualKeyPressed<&'static str>>) {
24    println!("key pressed: {}", virtual_key_pressed.key);
25}
26
27fn setup(mut commands: Commands) {
28    // ui camera
29    commands.spawn(Camera2d);
30
31    let layout = [
32        vec!["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", ".", ","],
33        vec!["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"],
34        vec!["A", "S", "D", "F", "G", "H", "J", "K", "L", "'"],
35        vec!["Z", "X", "C", "V", "B", "N", "M", "-", "/"],
36        vec!["space", "enter", "backspace"],
37        vec!["left", "right", "up", "down", "home", "end"],
38    ];
39
40    commands.spawn((
41        Node {
42            width: percent(100),
43            height: percent(100),
44            align_items: AlignItems::End,
45            justify_content: JustifyContent::Center,
46            ..default()
47        },
48        children![(
49            Node {
50                flex_direction: FlexDirection::Column,
51                border: px(5).into(),
52                row_gap: px(5),
53                padding: px(5).into(),
54                align_items: AlignItems::Center,
55                margin: px(25).into(),
56                border_radius: BorderRadius::all(px(10)),
57                ..Default::default()
58            },
59            BackgroundColor(NAVY.into()),
60            BorderColor::all(Color::WHITE),
61            children![
62                Text::new("virtual keyboard"),
63                (
64                    virtual_keyboard(layout.into_iter()),
65                    observe(on_virtual_key_pressed)
66                )
67            ]
68        )],
69    ));
70}