Prevent the default browser behavior of events (#1576)

This stops things like page scrolling on spacebar / arrow keys
This commit is contained in:
Ryan G 2020-05-21 13:13:33 -04:00 committed by GitHub
parent 49bcec1d27
commit 6cfddfea21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 3 deletions

View file

@ -1,4 +1,5 @@
# Unreleased
- On Web, prevent the webpage from scrolling when the user is focused on a winit canvas
# 0.22.2 (2020-05-16)

View file

@ -10,9 +10,9 @@ use stdweb::js;
use stdweb::traits::IPointerEvent;
use stdweb::unstable::TryInto;
use stdweb::web::event::{
BlurEvent, ConcreteEvent, FocusEvent, FullscreenChangeEvent, KeyDownEvent, KeyPressEvent,
KeyUpEvent, MouseWheelEvent, PointerDownEvent, PointerMoveEvent, PointerOutEvent,
PointerOverEvent, PointerUpEvent,
BlurEvent, ConcreteEvent, FocusEvent, FullscreenChangeEvent, IEvent, KeyDownEvent,
KeyPressEvent, KeyUpEvent, MouseWheelEvent, PointerDownEvent, PointerMoveEvent,
PointerOutEvent, PointerOverEvent, PointerUpEvent,
};
use stdweb::web::html_element::CanvasElement;
use stdweb::web::{
@ -130,6 +130,7 @@ impl Canvas {
F: 'static + FnMut(ScanCode, Option<VirtualKeyCode>, ModifiersState),
{
self.on_keyboard_release = Some(self.add_user_event(move |event: KeyUpEvent| {
event.prevent_default();
handler(
event::scan_code(&event),
event::virtual_key_code(&event),
@ -143,6 +144,7 @@ impl Canvas {
F: 'static + FnMut(ScanCode, Option<VirtualKeyCode>, ModifiersState),
{
self.on_keyboard_press = Some(self.add_user_event(move |event: KeyDownEvent| {
event.prevent_default();
handler(
event::scan_code(&event),
event::virtual_key_code(&event),
@ -228,6 +230,7 @@ impl Canvas {
F: 'static + FnMut(i32, MouseScrollDelta, ModifiersState),
{
self.on_mouse_wheel = Some(self.add_event(move |event: MouseWheelEvent| {
event.prevent_default();
if let Some(delta) = event::mouse_scroll_delta(&event) {
handler(0, delta, event::mouse_modifiers(&event));
}

View file

@ -135,6 +135,7 @@ impl Canvas {
{
self.on_keyboard_release =
Some(self.add_user_event("keyup", move |event: KeyboardEvent| {
event.prevent_default();
handler(
event::scan_code(&event),
event::virtual_key_code(&event),
@ -149,6 +150,7 @@ impl Canvas {
{
self.on_keyboard_press =
Some(self.add_user_event("keydown", move |event: KeyboardEvent| {
event.prevent_default();
handler(
event::scan_code(&event),
event::virtual_key_code(&event),
@ -242,6 +244,7 @@ impl Canvas {
F: 'static + FnMut(i32, MouseScrollDelta, ModifiersState),
{
self.on_mouse_wheel = Some(self.add_event("wheel", move |event: WheelEvent| {
event.prevent_default();
if let Some(delta) = event::mouse_scroll_delta(&event) {
handler(0, delta, event::mouse_modifiers(&event));
}