Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keyboard handling example and update documentation #294

Merged
merged 5 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/keyboard_handler/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "keyboard_handler"
edition = "2021"
license.workspace = true
version.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
floem = { path = "../.." }
31 changes: 31 additions & 0 deletions examples/keyboard_handler/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use floem::{
event::{Event, EventListener},
keyboard::Key,
unit::UnitExt,
view::View,
views::{stack, text, Decorators},
};

fn app_view() -> impl View {
let view =
stack((text("Example: Keyboard event handler").style(|s| s.padding(10.0)),)).style(|s| {
s.size(100.pct(), 100.pct())
.flex_col()
.items_center()
.justify_center()
});
view.keyboard_navigatable()
.on_event_stop(EventListener::KeyDown, move |e| {
if let Event::KeyDown(e) = e {
if e.key.logical_key == Key::Character("q".into()) {
println!("Goodbye :)");
std::process::exit(0)
}
println!("Key pressed in KeyCode: {:?}", e.key.physical_key);
}
})
}

fn main() {
floem::launch(app_view);
}
4 changes: 4 additions & 0 deletions src/views/decorator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ pub trait Decorators: View + Sized {
}

/// Add an handler for pressing down a specific key.
///
/// NOTE: View should have `.keyboard_navigable()` in order to receive keyboard events
fn on_key_down(
mut self,
key: Key,
Expand All @@ -112,6 +114,8 @@ pub trait Decorators: View + Sized {
}

/// Add an handler for a specific key being released.
///
/// NOTE: View should have `.keyboard_navigable()` in order to receive keyboard events
fn on_key_up(
mut self,
key: Key,
Expand Down
Loading