Open
Description
Bevy version
0.10.1
What you did
Using examples/input/mouse_grab.rs
:
fn grab_mouse(
mut windows: Query<&mut Window>,
mouse: Res<Input<MouseButton>>,
key: Res<Input<KeyCode>>,
) {
let mut window = windows.single_mut();
if mouse.just_pressed(MouseButton::Left) {
window.cursor.visible = false;
window.cursor.grab_mode = CursorGrabMode::Locked;
}
if key.just_pressed(KeyCode::Escape) {
window.cursor.visible = true;
window.cursor.grab_mode = CursorGrabMode::None;
}
}
Left clicking on the canvas will lock the cursor. Hitting escape will unlock the cursor.
However, this is handled by the browser only, and the Escape key event is never actually sent to bevy. window.cursor.grab_mode
will still be Locked
. Left-clicking at this point will do nothing, as grab_mode
is already Locked
and no state is changed.
Clicking Hitting escape a second time will then set grab_mode = None
, allowing you to re-lock by clicking.
What went wrong
- The browser detaching your cursor should be picked up by bevy and grab_mode should be set to None, or at the very least an event should be emitted.
Additional information
- In JS, you can listen to the change with
document.addEventListener('pointerlockchange', /* ... */)