Skip to main content

virtual_keyboard/
virtual_keyboard.rs

1//! Virtual keyboard example
2
3use bevy::{
4    color::palettes::css::NAVY,
5    feathers::{
6        controls::{VirtualKeyPressed, VirtualKeyboard},
7        dark_theme::create_dark_theme,
8        theme::UiTheme,
9        FeathersPlugins,
10    },
11    prelude::*,
12};
13
14fn main() {
15    App::new()
16        .add_plugins((DefaultPlugins, FeathersPlugins))
17        .insert_resource(UiTheme(create_dark_theme()))
18        .add_systems(Startup, scene.spawn())
19        .run();
20}
21
22fn on_virtual_key_pressed(virtual_key_pressed: On<VirtualKeyPressed<&'static str>>) {
23    println!("key pressed: {}", virtual_key_pressed.key);
24}
25
26fn scene() -> impl SceneList {
27    bsn_list![Camera2d, keyboard()]
28}
29
30fn keyboard() -> impl Scene {
31    let keys = [
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    bsn! {
41        Node {
42            width: percent(100),
43            height: percent(100),
44            align_items: AlignItems::End,
45            justify_content: JustifyContent::Center,
46        }
47        Children [(
48            Node {
49                flex_direction: FlexDirection::Column,
50                border: px(5),
51                row_gap: px(5),
52                padding: px(5),
53                align_items: AlignItems::Center,
54                margin: px(25),
55                border_radius: BorderRadius::all(px(10)),
56            }
57            BackgroundColor(NAVY)
58            BorderColor::all(Color::WHITE)
59            Children [
60                Text("virtual keyboard"),
61                (
62                    @VirtualKeyboard::<&str> { @keys: keys }
63                    on(on_virtual_key_pressed)
64                )
65            ]
66        )]
67    }
68}