mirror of
https://github.com/italicsjenga/winit-sonoma-fix.git
synced 2025-01-23 02:16:33 +11:00
Fix WindowEvent::ReceivedCharacter on web (#1747)
* Fix WindowEvent::ReceivedCharacter on web The event was never sent to the application because of the unconditional preventDefault() call on keydown. Fixes #1741 * Don't scroll when pressing space on a focused canvas After reaching keypress, we should prevent further propagation. Relates to #1741
This commit is contained in:
parent
66c117e599
commit
33fb62bb25
3 changed files with 30 additions and 5 deletions
|
@ -7,6 +7,7 @@
|
||||||
- On Wayland, fix window not being resizeable when using `with_min_inner_size` in `WindowBuilder`.
|
- On Wayland, fix window not being resizeable when using `with_min_inner_size` in `WindowBuilder`.
|
||||||
- On Unix, fix cross-compiling to wasm32 without enabling X11 or Wayland.
|
- On Unix, fix cross-compiling to wasm32 without enabling X11 or Wayland.
|
||||||
- On Windows, fix use after free crash during window destruction.
|
- On Windows, fix use after free crash during window destruction.
|
||||||
|
- On Web, fix `WindowEvent::ReceivedCharacter` never being sent on key input.
|
||||||
|
|
||||||
# 0.23.0 (2020-10-02)
|
# 0.23.0 (2020-10-02)
|
||||||
|
|
||||||
|
|
|
@ -10,9 +10,9 @@ use stdweb::js;
|
||||||
use stdweb::traits::IPointerEvent;
|
use stdweb::traits::IPointerEvent;
|
||||||
use stdweb::unstable::TryInto;
|
use stdweb::unstable::TryInto;
|
||||||
use stdweb::web::event::{
|
use stdweb::web::event::{
|
||||||
BlurEvent, ConcreteEvent, FocusEvent, FullscreenChangeEvent, IEvent, KeyDownEvent,
|
BlurEvent, ConcreteEvent, FocusEvent, FullscreenChangeEvent, IEvent, IKeyboardEvent,
|
||||||
KeyPressEvent, KeyUpEvent, MouseWheelEvent, PointerDownEvent, PointerMoveEvent,
|
KeyDownEvent, KeyPressEvent, KeyUpEvent, ModifierKey, MouseWheelEvent, PointerDownEvent,
|
||||||
PointerOutEvent, PointerOverEvent, PointerUpEvent,
|
PointerMoveEvent, PointerOutEvent, PointerOverEvent, PointerUpEvent,
|
||||||
};
|
};
|
||||||
use stdweb::web::html_element::CanvasElement;
|
use stdweb::web::html_element::CanvasElement;
|
||||||
use stdweb::web::{document, EventListenerHandle, IElement, IEventTarget, IHtmlElement};
|
use stdweb::web::{document, EventListenerHandle, IElement, IEventTarget, IHtmlElement};
|
||||||
|
@ -136,7 +136,17 @@ impl Canvas {
|
||||||
F: 'static + FnMut(ScanCode, Option<VirtualKeyCode>, ModifiersState),
|
F: 'static + FnMut(ScanCode, Option<VirtualKeyCode>, ModifiersState),
|
||||||
{
|
{
|
||||||
self.on_keyboard_press = Some(self.add_user_event(move |event: KeyDownEvent| {
|
self.on_keyboard_press = Some(self.add_user_event(move |event: KeyDownEvent| {
|
||||||
event.prevent_default();
|
// event.prevent_default() would suppress subsequent on_received_character() calls. That
|
||||||
|
// supression is correct for key sequences like Tab/Shift-Tab, Ctrl+R, PgUp/Down to
|
||||||
|
// scroll, etc. We should not do it for key sequences that result in meaningful character
|
||||||
|
// input though.
|
||||||
|
let event_key = &event.key();
|
||||||
|
let is_key_string = event_key.len() == 1 || !event_key.is_ascii();
|
||||||
|
let is_shortcut_modifiers = (event.ctrl_key() || event.alt_key())
|
||||||
|
&& !event.get_modifier_state(ModifierKey::AltGr);
|
||||||
|
if !is_key_string || is_shortcut_modifiers {
|
||||||
|
event.prevent_default();
|
||||||
|
}
|
||||||
handler(
|
handler(
|
||||||
event::scan_code(&event),
|
event::scan_code(&event),
|
||||||
event::virtual_key_code(&event),
|
event::virtual_key_code(&event),
|
||||||
|
@ -155,6 +165,8 @@ impl Canvas {
|
||||||
// viable/compatible alternative as of now. `beforeinput` is still widely
|
// viable/compatible alternative as of now. `beforeinput` is still widely
|
||||||
// unsupported.
|
// unsupported.
|
||||||
self.on_received_character = Some(self.add_user_event(move |event: KeyPressEvent| {
|
self.on_received_character = Some(self.add_user_event(move |event: KeyPressEvent| {
|
||||||
|
// Supress further handling to stop keys like the space key from scrolling the page.
|
||||||
|
event.prevent_default();
|
||||||
handler(event::codepoint(&event));
|
handler(event::codepoint(&event));
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
|
@ -157,7 +157,17 @@ impl Canvas {
|
||||||
self.on_keyboard_press = Some(self.common.add_user_event(
|
self.on_keyboard_press = Some(self.common.add_user_event(
|
||||||
"keydown",
|
"keydown",
|
||||||
move |event: KeyboardEvent| {
|
move |event: KeyboardEvent| {
|
||||||
event.prevent_default();
|
// event.prevent_default() would suppress subsequent on_received_character() calls. That
|
||||||
|
// supression is correct for key sequences like Tab/Shift-Tab, Ctrl+R, PgUp/Down to
|
||||||
|
// scroll, etc. We should not do it for key sequences that result in meaningful character
|
||||||
|
// input though.
|
||||||
|
let event_key = &event.key();
|
||||||
|
let is_key_string = event_key.len() == 1 || !event_key.is_ascii();
|
||||||
|
let is_shortcut_modifiers =
|
||||||
|
(event.ctrl_key() || event.alt_key()) && !event.get_modifier_state("AltGr");
|
||||||
|
if !is_key_string || is_shortcut_modifiers {
|
||||||
|
event.prevent_default();
|
||||||
|
}
|
||||||
handler(
|
handler(
|
||||||
event::scan_code(&event),
|
event::scan_code(&event),
|
||||||
event::virtual_key_code(&event),
|
event::virtual_key_code(&event),
|
||||||
|
@ -179,6 +189,8 @@ impl Canvas {
|
||||||
self.on_received_character = Some(self.common.add_user_event(
|
self.on_received_character = Some(self.common.add_user_event(
|
||||||
"keypress",
|
"keypress",
|
||||||
move |event: KeyboardEvent| {
|
move |event: KeyboardEvent| {
|
||||||
|
// Supress further handling to stop keys like the space key from scrolling the page.
|
||||||
|
event.prevent_default();
|
||||||
handler(event::codepoint(&event));
|
handler(event::codepoint(&event));
|
||||||
},
|
},
|
||||||
));
|
));
|
||||||
|
|
Loading…
Add table
Reference in a new issue