1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-02 06:40:06 -04:00

Add Ui.input_mut & InputState.ignore_key (#1212)

This commit is contained in:
cat-state
2022-02-15 16:14:24 +00:00
committed by GitHub
parent c8c871fcd1
commit c1569ed0d7
5 changed files with 86 additions and 48 deletions

View File

@@ -264,6 +264,42 @@ pub struct Modifiers {
}
impl Modifiers {
pub fn new() -> Self {
Default::default()
}
pub fn alt(self, value: bool) -> Self {
Self { alt: value, ..self }
}
pub fn ctrl(self, value: bool) -> Self {
Self {
ctrl: value,
..self
}
}
pub fn shift(self, value: bool) -> Self {
Self {
shift: value,
..self
}
}
pub fn mac_cmd(self, value: bool) -> Self {
Self {
mac_cmd: value,
..self
}
}
pub fn command(self, value: bool) -> Self {
Self {
command: value,
..self
}
}
#[inline(always)]
pub fn is_none(&self) -> bool {
self == &Self::default()

View File

@@ -192,6 +192,24 @@ impl InputState {
self.pointer.wants_repaint() || self.scroll_delta != Vec2::ZERO || !self.events.is_empty()
}
/// Ignore a key if it was pressed or released this frame. Useful for hotkeys.
/// Matches on both key press and key release, consuming them and removing them from `self.events`.
/// Returns true if the key was pressed this frame (even if the key release was consumed).
pub fn consume_key(&mut self, modifiers: Modifiers, key: Key) -> bool {
self.events.retain(|event| {
!matches!(
event,
Event::Key {
key: ev_key,
modifiers: ev_mods,
..
} if *ev_key == key && *ev_mods == modifiers
)
});
self.keys_down.remove(&key)
}
/// Was the given key pressed this frame?
pub fn key_pressed(&self, desired_key: Key) -> bool {
self.num_presses(desired_key) > 0

View File

@@ -338,6 +338,21 @@ impl Ui {
self.ctx().input()
}
/// The [`InputState`] of the [`Context`] associated with this [`Ui`].
/// Equivalent to `.ctx().input_mut()`.
///
/// Note that this locks the [`Context`], so be careful with if-let bindings
/// like for [`Self::input()`].
/// ```
/// # egui::__run_test_ui(|ui| {
/// ui.input_mut().consume_key(egui::Modifiers::default(), egui::Key::Enter);
/// # });
/// ```
#[inline]
pub fn input_mut(&self) -> RwLockWriteGuard<'_, InputState> {
self.ctx().input_mut()
}
/// The [`Memory`] of the [`Context`] associated with this ui.
/// Equivalent to `.ctx().memory()`.
#[inline]