From ea56b821933aafd32a6157383948dffdc059a0b6 Mon Sep 17 00:00:00 2001 From: Johan Klokkhammer Helsing Date: Sun, 5 Feb 2023 13:22:39 +0100 Subject: [PATCH 1/2] web: Fix Enter key being handled as character E When with_prevent_default is set to false, browsers will send keypress events for the enter key, with event.key set to Enter. Ignore it by checking the length of the key string. --- CHANGELOG.md | 2 ++ src/platform_impl/web/web_sys/canvas.rs | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a44a18bbab..5ed854510d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ And please only add new entries to the top of this list, right below the `# Unre # Unreleased +- On Web, fix Enter key being handled as the character E if `with_prevent_default` is false. + # 0.28.1 - On Wayland, fix crash when dropping a window in multi-window setup. diff --git a/src/platform_impl/web/web_sys/canvas.rs b/src/platform_impl/web/web_sys/canvas.rs index 1157a4f2d0..5a03ec51e3 100644 --- a/src/platform_impl/web/web_sys/canvas.rs +++ b/src/platform_impl/web/web_sys/canvas.rs @@ -239,6 +239,11 @@ impl Canvas { event.prevent_default(); } + if event.key().len() > 1 { + // ignore keypress for "Enter" key etc. + return; + } + handler(event::codepoint(&event)); }, )); From f62779574de0f99c82f558b5c6c60ad21420206b Mon Sep 17 00:00:00 2001 From: Johan Klokkhammer Helsing Date: Mon, 6 Feb 2023 21:59:19 +0100 Subject: [PATCH 2/2] Don't ignore unicode characters --- src/platform_impl/web/web_sys/canvas.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/platform_impl/web/web_sys/canvas.rs b/src/platform_impl/web/web_sys/canvas.rs index 5a03ec51e3..0851d18904 100644 --- a/src/platform_impl/web/web_sys/canvas.rs +++ b/src/platform_impl/web/web_sys/canvas.rs @@ -239,7 +239,7 @@ impl Canvas { event.prevent_default(); } - if event.key().len() > 1 { + if event.key().chars().nth(1).is_some() { // ignore keypress for "Enter" key etc. return; }