1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 13:20:05 -04:00
Files
egui/examples/keyboard_events/src/main.rs
Emil Ernerfeldt 27559ef3fd Rename Panel methods (#8192)
The three methods for showing a `Panel` are now:

* `panel.show`: always show the panel.
* `panel.show_collapsible`: show or hide the panel, with a slide
animation in between.
* `Panel::show_switched`: animate between two different panels: a
thin/collapsed one and a thick/expanded one.
2026-05-24 12:22:32 +02:00

49 lines
1.5 KiB
Rust

#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
#![expect(rustdoc::missing_crate_level_docs)] // it's an example
use eframe::egui;
use egui::{Key, ScrollArea};
fn main() -> eframe::Result {
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
let options = eframe::NativeOptions::default();
eframe::run_native(
"Keyboard events",
options,
Box::new(|_cc| Ok(Box::<Content>::default())),
)
}
#[derive(Default)]
struct Content {
text: String,
}
impl eframe::App for Content {
fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ui, |ui| {
ui.heading("Press/Hold/Release example. Press A to test.");
if ui.button("Clear").clicked() {
self.text.clear();
}
ScrollArea::vertical()
.auto_shrink(false)
.stick_to_bottom(true)
.show(ui, |ui| {
ui.label(&self.text);
});
if ui.input(|i| i.key_pressed(Key::A)) {
self.text.push_str("\nPressed");
}
if ui.input(|i| i.key_down(Key::A)) {
self.text.push_str("\nHeld");
ui.request_repaint(); // make sure we note the holding.
}
if ui.input(|i| i.key_released(Key::A)) {
self.text.push_str("\nReleased");
}
});
}
}