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 Ui.input_mut & InputState.ignore_key #1212

Merged
merged 15 commits into from
Feb 15, 2022
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w


## Unreleased
* `Ui::input_mut` to modify how subsequent widgets see the `InputState` and a convenience `InputState::ignore_key` for shortcuts or hotkeys.

### Added ⭐
* `Ui::input_mut` to modify how subsequent widgets see the `InputState` and a convenience `InputState::ignore_key` for shortcuts or hotkeys ([#1212](https://github.com/emilk/egui/pull/1212)).
cat-state marked this conversation as resolved.
Show resolved Hide resolved
* Much improved font selection ([#1154](https://github.com/emilk/egui/pull/1154)):
* You can now select any font size and family using `RichText::size` amd `RichText::family` and the new `FontId`.
* Easily change text styles with `Style::text_styles`.
Expand Down
14 changes: 7 additions & 7 deletions egui/src/input_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,19 +194,19 @@ impl InputState {

// Ignore a key if it was pressed or released this frame. Useful for hotkeys.
// Returns if the key was pressed this frame
pub fn ignore_key(&mut self, ignore_key: Key, modifiers: Modifiers) -> bool {
self.events = std::mem::take(&mut self.events).into_iter().filter(|event| {
pub fn consume_key(&mut self, key: Key, modifiers: Modifiers) -> bool {
self.events.retain(|event| {
!matches!(
event,
Event::Key {
key,
modifiers: mods,
key: ev_key,
modifiers: ev_mods,
..
} if *key == ignore_key && *mods == modifiers
} if *ev_key == key && *ev_mods == modifiers
)
}).collect();
});

self.keys_down.remove(&ignore_key)
self.keys_down.remove(&key)
}

/// Was the given key pressed this frame?
Expand Down
2 changes: 1 addition & 1 deletion egui/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ impl Ui {
/// like for [`Self::input()`].
/// ```
/// # egui::__run_test_ui(|ui| {
/// ui.input_mut().time = 0.0;
/// ui.input_mut().consume_key(egui::Key::Enter, egui::Modifiers::default());
/// # });
/// ```
#[inline]
Expand Down