Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/input/mouse_grab.rs
6595 views
1
//! Demonstrates how to grab and hide the mouse cursor.
2
3
use bevy::{
4
prelude::*,
5
window::{CursorGrabMode, CursorOptions},
6
};
7
8
fn main() {
9
App::new()
10
.add_plugins(DefaultPlugins)
11
.add_systems(Update, grab_mouse)
12
.run();
13
}
14
15
// This system grabs the mouse when the left mouse button is pressed
16
// and releases it when the escape key is pressed
17
fn grab_mouse(
18
mut cursor_options: Single<&mut CursorOptions>,
19
mouse: Res<ButtonInput<MouseButton>>,
20
key: Res<ButtonInput<KeyCode>>,
21
) {
22
if mouse.just_pressed(MouseButton::Left) {
23
cursor_options.visible = false;
24
cursor_options.grab_mode = CursorGrabMode::Locked;
25
}
26
27
if key.just_pressed(KeyCode::Escape) {
28
cursor_options.visible = true;
29
cursor_options.grab_mode = CursorGrabMode::None;
30
}
31
}
32
33