1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-26 22:53:14 -04:00

Add example with key presses

This commit is contained in:
Jose Palazon
2022-11-22 14:48:46 +00:00
parent 324104c7c3
commit ed0d5f871c
3 changed files with 72 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
[package]
name = "keyboard_events"
version = "0.1.0"
authors = ["Jose Palazon <jose@palako.com>"]
license = "MIT OR Apache-2.0"
edition = "2021"
rust-version = "1.65"
publish = false
[dependencies]
eframe = { path = "../../crates/eframe" }
tracing-subscriber = "0.3"

View File

@@ -0,0 +1,3 @@
```sh
cargo run -p hello_world
```

View File

@@ -0,0 +1,56 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
use eframe::egui;
use egui::*;
fn main() {
// Log to stdout (if you run with `RUST_LOG=debug`).
tracing_subscriber::fmt::init();
let options = eframe::NativeOptions::default();
eframe::run_native(
"Keyboard events",
options,
Box::new(|_cc| Box::new(Content::default())),
);
}
struct Content {
text: String,
}
impl Default for Content {
fn default() -> Self {
Self {
text: "".to_owned(),
}
}
}
impl eframe::App for Content {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("Press/Hold/Release example");
let text_style = TextStyle::Body;
let row_height = ui.text_style_height(&text_style);
ScrollArea::vertical()
.auto_shrink([false; 2])
.stick_to_bottom(true)
.show_rows(ui, row_height, self.text.len(), |ui, _row_range| {
//for row in row_range {
for line in self.text.lines() {
ui.label(line);
}
});
if ctx.input().key_released(Key::A) {
self.text.push_str("\nReleased");
}
if ctx.input().key_pressed(Key::A) {
self.text.push_str("\npressed");
}
if ctx.input().key_down(Key::A) {
self.text.push_str("\nheld");
}
});
}
}