Is there a way to get user input while the window is not in focus?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By neoxid501

I would like to make an application that is more or less a visualizer that responds to key pressed, but for that to work it can’t be in focus (specifically as a windows exe).

Is there a way to have Godot register user input while the window is not in focus?


:bust_in_silhouette: Reply From: fkul

If you mean have it accept input while another non-Godot application is focused, then no. Your OS’s window manager handles focus, applications like Godot can only request focus and request to be unfocused, and no well-written window manager would allow this behavior.

Not only would it be a huge security risk (any program could be a keylogger), but it’s also annoying to users, and may cause unnecessary bug reports. A user might wonder why a certain program isn’t accepting their input and file a bug report for that application, but it’s a completely different application grabbing their input. If you have several of these, you may have to reboot to regain control of your system, as each would randomly grab inputs depending on whether they were active or not, so quitting them may be difficult, especially if you’re not aware of what’s going on behind the scenes.

A way around this is to run a program from within another program (this is what debuggers and stack tracers do) in which case the parent accepts the input and transfers it to the child program. However, Godot is only capable of asking the OS to start a non-Godot program, the program runs independently of Godot, so you can’t intercept input this way, either. In fact, the only inter-process communication (IPC) that I know Godot uses is when it’s communicating to a running game, so you can change things in the editor as the game runs.

Gamepad input seems to still go to my game while the editor is focused, at least (but definitely not keyboard input).

guywithknife | 2018-03-15 11:31

I hit this topic while searching for the same thing. The answer doesn’t seem to make sense since there are many programs that accept hotkey presses outside of focus, like OBS. But if Godot can’t do that, then ok.

HeroicNate | 2020-10-08 15:55