Repository: godotengine/godot (language: cpp)

Issue:
[Web] `Input.mouse_mode` can't capture mouse after user presses escape key in browser
### Tested versions

- Reproducible in Godot 4.4.beta2
- Not Reproducible in 4.3

### System information

Godot v4.4.beta2 - Linux Mint 22 (Wilma) on X11 - X11 display driver, Multi-window, 2 monitors - OpenGL 3 (Compatibility) - NVIDIA GeForce RTX 4060 Ti (nvidia; 550.120) - AMD Ryzen 9 3900X 12-Core Processor (24 threads)

### Issue description

I have discovered an edge case that likely happens often. When the mouse is captured browsers tend to tell the user that they can press escape to free there mouse. If we capture the users mouse and then they press escape we can not again capture it with the current behavior. Even though the engine knows that the mouse has been released. To capture the mouse again after the user has pressed escape we must first tell the engine to release the mouse and then capture it again. So that they engine executes the correct sequence.

Note: https://github.com/godotengine/godot/issues/100758#issuecomment-2624679816

### Steps to reproduce


1. Extract MRP
2. Open MRP in Godot 4.4.beta2
3. Ensure exports are managed for Godot 4.4.beta2
4. Run the MRP in the browser.
5. Capture the mouse using the button provided.
6. Press the escape key "esc".
7. Observe that your mouse is released and visible.
8. Attempt to capture the mouse using the button provided that says "Capture Mouse".
9. Observe this does not work.
10. Press the button that says "Capture Mouse After Release".
11. Observe this works.

Alternatively for step 10. You can also press space bar or press the "Release Mouse" button. and then you can press the "Capture Mouse" button and it will work.

### Minimal reproduction project (MRP)

[web-mouse-capture-part-2.zip](https://github.com/user-attachments/files/18608637/web-mouse-capture-part-2.zip)


Discussion / hints:

Fix patch:
diff --git a/platform/web/display_server_web.cpp b/platform/web/display_server_web.cpp
index 89cc6339d28b..180c05ef411d 100644
--- a/platform/web/display_server_web.cpp
+++ b/platform/web/display_server_web.cpp
@@ -576,9 +576,17 @@ void DisplayServerWeb::_mouse_update_mode() {
 
 void DisplayServerWeb::mouse_set_mode(MouseMode p_mode) {
 	ERR_FAIL_INDEX(p_mode, MouseMode::MOUSE_MODE_MAX);
-	if (p_mode == mouse_mode_base) {
+
+	if (mouse_mode_override_enabled) {
+		mouse_mode_base = p_mode;
+		// No need to update, as override is enabled.
+		return;
+	}
+	if (p_mode == mouse_mode_base && p_mode == mouse_get_mode()) {
+		// No need to update, as it is currently set as the correct mode.
 		return;
 	}
+
 	mouse_mode_base = p_mode;
 	_mouse_update_mode();
 }
@@ -596,9 +604,17 @@ DisplayServer::MouseMode DisplayServerWeb::mouse_get_mode() const {
 
 void DisplayServerWeb::mouse_set_mode_override(MouseMode p_mode) {
 	ERR_FAIL_INDEX(p_mode, MouseMode::MOUSE_MODE_MAX);
-	if (p_mode == mouse_mode_override) {
+
+	if (!mouse_mode_override_enabled) {
+		mouse_mode_override = p_mode;
+		// No need to update, as override is not enabled.
+		return;
+	}
+	if (p_mode == mouse_mode_override && p_mode == mouse_get_mode()) {
+		// No need to update, as it is currently set as the correct mode.
 		return;
 	}
+
 	mouse_mode_override = p_mode;
 	_mouse_update_mode();
 }


Test patch: