How to make a global warp_mouse()?

Godot Version

4.6.2

Question

I am attempting to make a mini application that shakes your mouse by adding and subtracting from it’s position randomly. My question is, how do i make a mouse_warp() function that works properly whilst focused on any window? right now, once i click off of the root scene, my mouse goes to a corner and stays there.

code for reference:

    var CurrentLevel = 5.0
	var TemporaryScreenOrigin = Vector2i(0,0) - get_tree().get_root().position
	var TemporaryMousePosition = DisplayServer.mouse_get_position()
	get_tree().get_root().warp_mouse(TemporaryScreenOrigin + TemporaryMousePosition + Vector2i(randi_range(-CurrentLevel,CurrentLevel),randi_range(-CurrentLevel,CurrentLevel)))

Have you looked at getting/setting the Viewport or Window and using warp_mouse() on that?

calling a warp_mouse() on a viewport only works whilst you are focused on that viewport’s window, focusing on any other tab will still cause the issue.

Maybe you can use get_window().grab_focus() before you need to use warp.

edit: nevermind, i checked for myself and this works, thank you !!

As per the forum etiquete,

If my solution works, please mark my post as the solution.

If you found another solution, please explain your solution here so other people would know how to solve this issue, if they ever encounter it.