Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/ui/virtual_keyboard.rs
6849 views
1
//! Virtual keyboard example
2
3
use bevy::{
4
color::palettes::css::NAVY,
5
ecs::relationship::RelatedSpawnerCommands,
6
feathers::{
7
controls::virtual_keyboard, dark_theme::create_dark_theme, theme::UiTheme, FeathersPlugins,
8
},
9
prelude::*,
10
ui_widgets::Activate,
11
};
12
13
fn main() {
14
App::new()
15
.add_plugins((DefaultPlugins, FeathersPlugins))
16
.insert_resource(UiTheme(create_dark_theme()))
17
.add_systems(Startup, setup)
18
.run();
19
}
20
21
#[derive(Component)]
22
struct VirtualKey(String);
23
24
fn on_virtual_key_pressed(
25
In(Activate(virtual_key_entity)): In<Activate>,
26
virtual_key_query: Query<&VirtualKey>,
27
) {
28
if let Ok(VirtualKey(label)) = virtual_key_query.get(virtual_key_entity) {
29
println!("key pressed: {label}");
30
}
31
}
32
33
fn setup(mut commands: Commands) {
34
// ui camera
35
commands.spawn(Camera2d);
36
let callback = commands.register_system(on_virtual_key_pressed);
37
38
let layout = [
39
vec!["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", ".", ","],
40
vec!["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"],
41
vec!["A", "S", "D", "F", "G", "H", "J", "K", "L", "'"],
42
vec!["Z", "X", "C", "V", "B", "N", "M", "-", "/"],
43
vec!["space", "enter", "backspace"],
44
vec!["left", "right", "up", "down", "home", "end"],
45
];
46
47
let keys_iter = layout.into_iter().map(|row| {
48
row.into_iter()
49
.map(|label| {
50
let label_string = label.to_string();
51
(label_string.clone(), VirtualKey(label_string))
52
})
53
.collect()
54
});
55
56
commands
57
.spawn(Node {
58
width: percent(100),
59
height: percent(100),
60
align_items: AlignItems::End,
61
justify_content: JustifyContent::Center,
62
..default()
63
})
64
.with_children(|parent: &mut RelatedSpawnerCommands<ChildOf>| {
65
parent
66
.spawn((
67
Node {
68
flex_direction: FlexDirection::Column,
69
border: px(5).into(),
70
row_gap: px(5),
71
padding: px(5).into(),
72
align_items: AlignItems::Center,
73
margin: px(25).into(),
74
..Default::default()
75
},
76
BackgroundColor(NAVY.into()),
77
BorderColor::all(Color::WHITE),
78
BorderRadius::all(px(10)),
79
))
80
.with_children(|parent: &mut RelatedSpawnerCommands<ChildOf>| {
81
parent.spawn(Text::new("virtual keyboard"));
82
parent.spawn(virtual_keyboard(keys_iter, callback));
83
});
84
});
85
}
86
87