Skip to content

Commit a9a1581

Browse files
johanhelsingItsDoot
authored andcommitted
Allow not preventing default event behaviors on wasm (bevyengine#7304)
# Objective On wasm, bevy applications currently prevent any of the normal browser hotkeys from working normally (Ctrl+R, F12, F5, Ctrl+F5, tab, etc.). Some of those events you may want to override, perhaps you can hold the tab key for showing in-game stats? However, if you want to make a well-behaved game, you probably don't want to needlessly prevent that behavior unless you have a good reason. Secondary motivation: Also, consider the workaround presented here to get audio working: https://developer.chrome.com/blog/web-audio-autoplay/#moving-forward ; It won't work (for keydown events) if we stop event propagation. ## Solution - Winit has a field that allows it to not stop event propagation, expose it on the window settings to allow the user to choose the desired behavior. Default to `true` for backwards compatibility. --- ## Changelog - Added `Window::prevent_default_event_handling` . This allows bevy apps to not override default browser behavior on hotkeys like F5, F12, Ctrl+R etc.
1 parent 19a56b2 commit a9a1581

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

crates/bevy_window/src/window.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ pub struct Window {
158158
///
159159
/// This value has no effect on non-web platforms.
160160
pub fit_canvas_to_parent: bool,
161+
/// Whether or not to stop events from propagating out of the canvas element
162+
///
163+
/// When `true`, this will prevent common browser hotkeys like F5, F12, Ctrl+R, tab, etc.
164+
/// from performing their default behavior while the bevy app has focus.
165+
///
166+
/// This value has no effect on non-web platforms.
167+
pub prevent_default_event_handling: bool,
161168
/// Stores internal state that isn't directly accessible.
162169
pub internal: InternalWindowState,
163170
}
@@ -180,6 +187,7 @@ impl Default for Window {
180187
focused: true,
181188
always_on_top: false,
182189
fit_canvas_to_parent: false,
190+
prevent_default_event_handling: true,
183191
canvas: None,
184192
}
185193
}

crates/bevy_winit/src/winit_windows.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ impl WinitWindows {
110110
panic!("Cannot find element: {}.", selector);
111111
}
112112
}
113+
114+
winit_window_builder =
115+
winit_window_builder.with_prevent_default(window.prevent_default_event_handling)
113116
}
114117

115118
let winit_window = winit_window_builder.build(event_loop).unwrap();

examples/window/window_settings.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ fn main() {
1616
present_mode: PresentMode::AutoVsync,
1717
// Tells wasm to resize the window according to the available canvas
1818
fit_canvas_to_parent: true,
19+
// Tells wasm not to override default event handling, like F5, Ctrl+R etc.
20+
prevent_default_event_handling: false,
1921
..default()
2022
}),
2123
..default()

0 commit comments

Comments
 (0)