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

Added KeyRepeat event. #2389

Merged
merged 1 commit into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions crates/egui/src/data/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ 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,
},

/// The mouse or touch moved to a new place.
PointerMoved(Pos2),

Expand Down
46 changes: 28 additions & 18 deletions crates/egui/src/input_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,26 +164,36 @@ impl InputState {
let mut keys_down = self.keys_down;
let mut scroll_delta = Vec2::ZERO;
let mut zoom_factor_delta = 1.0;
new.events.retain(|event| match event {
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)
keys_down.insert(*key)
} else {
keys_down.remove(key);
true
for event in &mut new.events {
match event {
Event::Key {
key,
pressed,
modifiers,
} => {
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,
};
}
} else {
keys_down.remove(key);
}
}
Event::Scroll(delta) => {
scroll_delta += *delta;
}
Event::Zoom(factor) => {
zoom_factor_delta *= *factor;
}
_ => {}
}
Event::Scroll(delta) => {
scroll_delta += *delta;
true
}
Event::Zoom(factor) => {
zoom_factor_delta *= *factor;
true
}
_ => true,
});
}

InputState {
pointer,
touch_states: self.touch_states,
Expand Down