How to grab focus in the running project?

Godot Version

4.3.dev3

Question

I have it set up so that my project runs on my second monitor in full screen mode.
Is there a way to focus the running project after pressing “Run Project (F5)” so that my inputs don’t register in the text editor? It’s very easy to miss.

func _ready() -> void:
	Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
	# make the game grab focus so I don't accidentally type "dddddaaaawssssddd" 
2 Likes

Try get_window().grab_focus()

2 Likes

Hmmm, this works but only if I don’t use full screen mode. When I change the mode back to fullscreen, the editor still registers keystrokes. Might this be a bug?

I works it’s fine for now but I would prefer fullscreen.

func _ready() -> void:
	Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
	get_window().grab_focus()

I don’t know exactly, I can’t replicate this myself so not sure if it’s a bug or something else going on

How were you able to get it to run on your second monitor in the first place? The default behavior for me is to open on the same monitor as the Godot editor always.

There’s an option in Editor Settings → Run → Window Placement → Screen.

1 Like

I threw together a quick minimum reproduction project and wasn’t able to replicate. Still was able to get focus and input in the running project when I ran it, didn’t need to use Input.mouse_mode or get_window().grab_focus(). Feel free to clone and test for yourself.

My environment is Windows 11 with Godot 4.1.1

this is happening to me on ubuntu, very annoying. game instance window should get focus after hitting run, but the script window keeps it so my game’s running in the foreground but all my WASD inputs go into the script window.