Skip to content

Commit

Permalink
Revert key-repeat behavior (#2429)
Browse files Browse the repository at this point in the history
* Revert key-repeat behavior

This fixes key-repeats everywhere in egui where it was broken,including:

- Enter in TextEdit:s
- Arrow keys for sliders and dragvalues
- …

* Update changelog

* Remove old comment
  • Loading branch information
emilk authored Dec 11, 2022
1 parent e7471f1 commit 0a1b85f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 30 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ NOTE: [`epaint`](crates/epaint/CHANGELOG.md), [`eframe`](crates/eframe/CHANGELOG


## Unreleased
### Changed 🔧
* `InputState`: all press functions again include key repeates (like in egui 0.19) ([#2429](https://github.com/emilk/egui/pull/2429)).

### Fixed 🐛
* Fix key-repeat for backspace and arrow keys in `TextEdit` ([#2416](https://github.com/emilk/egui/pull/2416)).
* Fix key-repeats for `TextEdit`, `Slider`s, etc ([#2429](https://github.com/emilk/egui/pull/2429)).


## 0.20.0 - 2022-12-08 - AccessKit, prettier text, overlapping widgets
Expand Down
12 changes: 5 additions & 7 deletions crates/egui/src/data/input.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! The input needed by egui.
#![allow(deprecated)] // TODO(emilk): remove

use crate::emath::*;

/// What the integrations provides to egui at the start of each frame.
Expand Down Expand Up @@ -191,13 +193,9 @@ pub enum Event {
modifiers: Modifiers,
},

/// A key was repeated while pressed.
KeyRepeat {
key: Key,

/// The state of the modifier keys at the time of the event.
modifiers: Modifiers,
},
/// DEPRECATED - DO NOT USE
#[deprecated = "Do not use"]
KeyRepeat { key: Key, modifiers: Modifiers },

/// The mouse or touch moved to a new place.
PointerMoved(Pos2),
Expand Down
33 changes: 15 additions & 18 deletions crates/egui/src/input_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,20 +166,10 @@ impl InputState {
let mut zoom_factor_delta = 1.0;
for event in &mut new.events {
match event {
Event::Key {
key,
pressed,
modifiers,
} => {
Event::Key { key, pressed, .. } => {
if *pressed {
// We only retain presses that are novel (i.e. the first Press event, not those generated by key-repeat)
// key repeats are represented by KeyRepeat.
if !keys_down.insert(*key) {
*event = Event::KeyRepeat {
key: *key,
modifiers: *modifiers,
};
}
keys_down.insert(*key);
// TODO(emilk): detect key repeats and mark the event accordingly!
} else {
keys_down.remove(key);
}
Expand Down Expand Up @@ -262,6 +252,8 @@ impl InputState {
}

/// Count presses of a key. If non-zero, the presses are consumed, so that this will only return non-zero once.
///
/// Includes key-repeat events.
pub fn count_and_consume_key(&mut self, modifiers: Modifiers, key: Key) -> usize {
let mut count = 0usize;

Expand All @@ -284,35 +276,40 @@ impl InputState {
}

/// Check for a key press. If found, `true` is returned and the key pressed is consumed, so that this will only return `true` once.
///
/// Includes key-repeat events.
pub fn consume_key(&mut self, modifiers: Modifiers, key: Key) -> bool {
self.count_and_consume_key(modifiers, key) > 0
}

/// Check if the given shortcut has been pressed.
///
/// If so, `true` is returned and the key pressed is consumed, so that this will only return `true` once.
///
/// Includes key-repeat events.
pub fn consume_shortcut(&mut self, shortcut: &KeyboardShortcut) -> bool {
let KeyboardShortcut { modifiers, key } = *shortcut;
self.consume_key(modifiers, key)
}

/// Was the given key pressed this frame?
///
/// Includes key-repeat events.
pub fn key_pressed(&self, desired_key: Key) -> bool {
self.num_presses(desired_key) > 0
}

/// How many times was the given key pressed this frame?
///
/// Includes key-repeat events.
pub fn num_presses(&self, desired_key: Key) -> usize {
self.events
.iter()
.filter(|event| {
matches!(
event,
Event::Key {
key,
pressed: true,
..
} if *key == desired_key
Event::Key { key, pressed: true, .. }
if *key == desired_key
)
})
.count()
Expand Down
4 changes: 0 additions & 4 deletions crates/egui/src/widgets/text_edit/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -919,10 +919,6 @@ fn events(
modifiers,
} => on_key_press(&mut cursor_range, text, galley, *key, modifiers),

Event::KeyRepeat { key, modifiers } => {
on_key_press(&mut cursor_range, text, galley, *key, modifiers)
}

Event::CompositionStart => {
state.has_ime = true;
None
Expand Down

0 comments on commit 0a1b85f

Please sign in to comment.