emilk / emilk/egui

consume_key and consume_shortcut with input field

Open
#5,338 2 comments 2 reactions 0 assignees View on GitHub
bug
Dominant language
Rust
Stars
30.6k
Forks
2.1k
Avg merge
1d 9h
Merged PRs (30d)
72

Description

**Describe the bug**
When a keyboard shortcut is consumed, the key will be inserted into an active edit field anyway.

In the below example, there is an edit field to type and independent of this, the keyboard shortcut ALT-B will be intercepted. In the example, the keyboard shortcut is processed before the edit field is displayed. Pressing ALT-B will trigger the action but will also insert the letter 'b' into the edit field.

**To Reproduce**
Steps to reproduce the behavior:

```rust
use eframe::egui;

fn main() -> eframe::Result {
eframe::run_native(
"xyz",
eframe::NativeOptions::default(),
Box::new(|_cc| Ok(Box::::default())),
)
}

#[derive(Default)]
pub struct MyApp {
content: String,
show_label: bool,
}

impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
if ctx.input_mut(|i| i.consume_key(egui::Modifiers::ALT, egui::Key::B)) {
self.show_label = true;
};
ui.label("Type a few chars and then press ALT-B:");
ui.text_edit_singleline(&mut self.content);
if self.show_label {
ui.label("You pressed ALT-B. Note that the char 'b' is inserted into the edit field, even though consume_key has been called.");
}
});
}
}
```

**Expected behavior**

I would expect that if the key is consumed it will not be interpreted by the text edit. This is also what the function description indicates: "Check for a key press. If found, `true` is returned and the key pressed is consumed, so that this will only return `true` once.".

**Screenshots**

![Bildschirmfoto_2024-11-01_16-57-03](https://github.com/user-attachments/assets/6d620bcd-442e-4c92-86ee-d99a5e08a2ca)

![Bildschirmfoto_2024-11-01_16-57-32](https://github.com/user-attachments/assets/a8df5dde-428f-4ca4-afbb-cd16cefc2878)

**Desktop (please complete the following information):**
- OS: ArchLinux

Contributor guide

Open the contributing guide

Research direction

Start by tracing egui's consume_key and consume_shortcut input handling alongside the text-edit widget, using the supplied Rust reproduction. Done means ALT-B triggers the shortcut without inserting “b” into the active edit field, while preserving the documented consumed-key behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.