File tree 3 files changed +31
-0
lines changed
3 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -430,6 +430,10 @@ path = "examples/input/mouse_input.rs"
430
430
name = " mouse_input_events"
431
431
path = " examples/input/mouse_input_events.rs"
432
432
433
+ [[example ]]
434
+ name = " mouse_grab"
435
+ path = " examples/input/mouse_grab.rs"
436
+
433
437
[[example ]]
434
438
name = " touch_input"
435
439
path = " examples/input/touch_input.rs"
Original file line number Diff line number Diff line change @@ -203,6 +203,7 @@ Example | File | Description
203
203
` keyboard_modifiers ` | [ ` input/keyboard_modifiers.rs ` ] ( ./input/keyboard_modifiers.rs ) | Demonstrates using key modifiers (ctrl, shift)
204
204
` mouse_input ` | [ ` input/mouse_input.rs ` ] ( ./input/mouse_input.rs ) | Demonstrates handling a mouse button press/release
205
205
` mouse_input_events ` | [ ` input/mouse_input_events.rs ` ] ( ./input/mouse_input_events.rs ) | Prints out all mouse events (buttons, movement, etc.)
206
+ ` mouse_grab ` | [ ` input/mouse_grab.rs ` ] ( ./input/mouse_grab.rs ) | Demonstrates how to grab the mouse, locking the cursor to the app's screen
206
207
` touch_input ` | [ ` input/touch_input.rs ` ] ( ./input/touch_input.rs ) | Displays touch presses, releases, and cancels
207
208
` touch_input_events ` | [ ` input/touch_input_events.rs ` ] ( ./input/touch_input_events.rs ) | Prints out all touch inputs
208
209
Original file line number Diff line number Diff line change
1
+ use bevy:: prelude:: * ;
2
+
3
+ fn main ( ) {
4
+ App :: new ( )
5
+ . add_plugins ( DefaultPlugins )
6
+ . add_system ( grab_mouse)
7
+ . run ( ) ;
8
+ }
9
+
10
+ // This system grabs the mouse when the left mouse button is pressed
11
+ // and releases it when the escape key is pressed
12
+ fn grab_mouse (
13
+ mut windows : ResMut < Windows > ,
14
+ mouse : Res < Input < MouseButton > > ,
15
+ key : Res < Input < KeyCode > > ,
16
+ ) {
17
+ let window = windows. get_primary_mut ( ) . unwrap ( ) ;
18
+ if mouse. just_pressed ( MouseButton :: Left ) {
19
+ window. set_cursor_visibility ( false ) ;
20
+ window. set_cursor_lock_mode ( true ) ;
21
+ }
22
+ if key. just_pressed ( KeyCode :: Escape ) {
23
+ window. set_cursor_visibility ( true ) ;
24
+ window. set_cursor_lock_mode ( false ) ;
25
+ }
26
+ }
You can’t perform that action at this time.
0 commit comments