Skip to content

Windows cursor grabbing is bugged #22750

@Saratii

Description

@Saratii

Bevy version and features

0.18 main branch

[Optional] Relevant system information

Windows 11

What you did

Ran the attached code, hit the windows key to change focus, tried to move the cursor out of the window.

What went wrong

If the debug print is showing that the cursor is not grabbed I expect the mouse to not act confined. It is not letting my mouse leave the window even though its not grabbed. I minimized the code for you.

Additional information

use std::time::Duration;

use bevy::{
    prelude::*,
    window::{CursorGrabMode, CursorOptions, PrimaryWindow, WindowFocused},
};

fn main() {
    App::new()
        .add_plugins(DefaultPlugins.set(WindowPlugin {
            primary_window: Some(Window { ..default() }),
            ..default()
        }))
        .add_systems(Update, (print_cursor, handle_focus_change))
        .run();
}

fn print_cursor(
    primary_cursor_options: Single<&mut CursorOptions, With<PrimaryWindow>>,
    mut time_since_last_print: Local<Duration>,
    time: Res<Time>,
) {
    *time_since_last_print += time.delta();
    if *time_since_last_print < Duration::from_secs(5) {
        return;
    }
    println!("grabbed: {:?}", primary_cursor_options.grab_mode);
    *time_since_last_print = Duration::ZERO;
}

fn handle_focus_change(
    mut focus_events: MessageReader<WindowFocused>,
    mut primary_cursor_options: Single<&mut CursorOptions, With<PrimaryWindow>>,
) {
    for event in focus_events.read() {
        if event.focused {
            println!("gained focus");
            primary_cursor_options.grab_mode = CursorGrabMode::Confined;
        } else {
            println!("lost focus");
            primary_cursor_options.grab_mode = CursorGrabMode::None;
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-WindowingPlatform-agnostic interface layer to run your app inC-BugAn unexpected or incorrect behaviorO-WindowsSpecific to the Windows desktop operating systemS-BlockedThis cannot move forward until something else changes

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions