eframe web: Fix stuck keys after pressing ctrl+C, cmd+A, etc (#4731)

* Closes https://github.com/emilk/egui/issues/4724
This commit is contained in:
Emil Ernerfeldt 2024-06-28 18:12:50 +02:00 committed by GitHub
parent 2180f16cba
commit d10b3c1f4e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 20 additions and 0 deletions

View File

@ -244,6 +244,7 @@ fn install_keyup(runner_ref: &WebRunner, target: &EventTarget) -> Result<(), JsV
pub(crate) fn on_keyup(event: web_sys::KeyboardEvent, runner: &mut AppRunner) {
let modifiers = modifiers_from_kb_event(&event);
runner.input.raw.modifiers = modifiers;
if let Some(key) = translate_key(&event.key()) {
runner.input.raw.events.push(egui::Event::Key {
key,
@ -253,6 +254,25 @@ pub(crate) fn on_keyup(event: web_sys::KeyboardEvent, runner: &mut AppRunner) {
modifiers,
});
}
if event.key() == "Meta" || event.key() == "Control" {
// When pressing Cmd+A (select all) or Ctrl+C (copy),
// chromium will not fire a `keyup` for the letter key.
// This leads to stuck keys, unless we do this hack.
// See https://github.com/emilk/egui/issues/4724
let keys_down = runner.egui_ctx().input(|i| i.keys_down.clone());
for key in keys_down {
runner.input.raw.events.push(egui::Event::Key {
key,
physical_key: None,
pressed: false,
repeat: false,
modifiers,
});
}
}
runner.needs_repaint.repaint_asap();
let has_focus = runner.input.raw.focused;