Skip to content

Cursor is caught in unfocused window #2757

@Bestsoft101

Description

@Bestsoft101

This issue is probably related to my previous one: #2756

When I create a GLFW window with GLFW_CURSOR set to GLFW_CURSOR_DISABLED, and focus another window, before the GLFW window is created, it locks the cursor in the position of the window, even though it's not at the top.

Here you can see me start the program 2 times:
The first time, I just run it, it creates a window and captures the cursor as expected.
The second time, I move the eclipse window before the GLFW window appears, and the cursor is captured. In this case I'm able to unlock the cursor by opening the start menu...

2025-08-12.02-13-38.mp4

...however in another project of mine I got an even worse version of this issue, where opening the start menu does not fix it. I wasn't able to recreate the issue in a smaller program though. If code to reproduce this is needed, let me know and I'll try it again.

2025-08-12.02-24-19.mp4

Code:

import static org.lwjgl.glfw.GLFW.*;

import org.lwjgl.glfw.GLFWVidMode;

public class Test {
	
	public static long handle;
	
	public static void main(String[] args) {
		try {
			Thread.sleep(500);
		}catch (Exception e) {}
		
		glfwInit();
		
		glfwDefaultWindowHints();
		glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
		
		handle = glfwCreateWindow(640, 480, "Test", 0, 0);
		
		centerWindow();
		
		glfwShowWindow(handle);
		
		boolean grabbed = false;
		long start = System.currentTimeMillis();
		
		while(true) {
			glfwPollEvents();
			
			if(glfwWindowShouldClose(handle)) {
				break;
			}
			
			// Grab the cursor one second after the window is created
			long now = System.currentTimeMillis();
			if(now - start > 1000 && !grabbed) {
				System.out.println("Grab mouse");
				
				grabMouse();
				grabbed = true;
			}
		}
	}
	
	public static void grabMouse() {
		glfwSetInputMode(handle, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
		centerCursor();
	}
	
	public static void centerCursor() {
		int[] w = new int[1];
		int[] h = new int[1];
		glfwGetWindowSize(handle, w, h);

		int x = w[0] / 2;
		int y = h[0] / 2;
		glfwSetCursorPos(handle, x, y);
	}
	
	public static void centerWindow() {
		long primaryMonitor = glfwGetPrimaryMonitor();
		
		GLFWVidMode vidMode = glfwGetVideoMode(primaryMonitor);

		int[] w = new int[1];
		int[] h = new int[1];
		glfwGetWindowSize(handle, w, h);

		int x = (vidMode.width() - w[0]) / 2;
		int y = (vidMode.height() - h[0]) / 2;
		
		glfwSetWindowPos(handle, x, y);
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    WindowsWin32 specific (not Cygwin or WSL)bugBug reports and bugfix pull requestscannot reproduceBugs that have failed verification

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions