Background Processing of Joystick/Gamepad Input when not in focus

Hi,

I’d like to be able to continue controlling the player even while the window is not in focus using a joystick or gamepad but doesn’t seem possible as it stands today. Is something that we could please get added? I’ve been testing on Windows but I suspect that it is the same case on Linux and Mac as well.

Any help on would be very much appreciated.

You always have your Input processing, probably you’re confused with Input mode concept.
You can pass your input data to UI only, Game Logic only or both if you want to.
In PlayerController look at Set Input Mode Game/UI/Game&UI nodes

Do you mean that joystick input is still being captured even when the Unreal window is not in focus and that by setting the input mode I can capture that? My intention is to still allow user movement in the game world even when the user has alt+tab’ed to another non-Unreal window on his computer.

You would need to modify the core files of UE4 that interact with the OS to always listen for input events to do (if that is even possible), but I would not recommend doing so. Overriding default behavior of the OS will create more problems than it’s worth. Potential security issues would be one reason as you could be opening a backdoor into the OS for hackers to exploit, and is generally something to avoid unless absolutely necessary.

I’m curious why you want to add that behavior though, are you launching a separate program along with the game?

is 100% not true. By default, programs will continue to receive controller input even when they do not have focus ( is not the case for keyboard events). Unreal will be choosing to ignore input somewhere in the engine code.

You can easily confirm by putting a breakpoint in the SendControllerEvents function that applies to you and then pushing a button. The engine will respond, but no input event is fired in the game.

I found thread while looking for the same thing as the OP. It’s super frustrating to try to debug input based blueprints since you can’t be looking at the blueprint when pressing the buttons/sticks. So if someone does know how to fix (or if they can even just point me to the code where Unreal discards the input events), I would appreciate it.

As time has passed, it may not be necessary anymore …

When focus lost UGameViewportClient :: LostFocus() is called.
At that time, APlayerController :: FlushPressedKeys () called and the inputs are erased.
You can do by overrideing function so that it does not clear the key.

2 Likes

Could you please explain what steps I would need to take in order to overload the function?

Was looking for the same thing.

Usually input is pumped from the Messages Queues. Input (especially keyboard input) will only be placed in the queue of the focused application.
.microsoft.com/en-us/win…message-queues

To retrieve events if the application is not focused, you can register hooks.
.microsoft.com/en-us/win…ow%20procedure.
Though the hooks I was able to register did not work for me when the application was in the background.

Never the less I was looking for events from my XBox controller, and the hooks did not even catch those when the application was in the foreground.
When you use the RawInput plugin, you can fix it by copying the plugin to your project and changing the code:

RawInputWindows.cpp



FRawInputWindows::FRawInputWindows(const TSharedRef<FGenericApplicationMessageHandler>& InMessageHandler)
: IRawInput(InMessageHandler)
{
...
   // Register a default device if desired
   if (GetDefault<URawInputSettings>()->bRegisterDefaultDevice)
   {
      const uint32 Flags = RIDEV_INPUTSINK;//0;        ** // CHANGE HERE**
      const int32 PageID = 0x01;
      int32 DeviceID = 0x04;
      DefaultDeviceHandle = RegisterInputDevice(RIM_TYPEHID, Flags, DeviceID, PageID);
...
}


For the Flags, see here: .microsoft.com/en-us/win…rawinputdevice

When running the Editor:
The editor has performance throttels when in the background which can cause problems with the input even though the input should be queued by the system (?!?!).
1: One severe when the editor is minimized: hard throtteling to 3FPS.
To fix it you need to create a custom UUnrealEdEngine and register it in DefaultEngine.ini config file.



[/Script/Engine.Engine]
UnrealEdEngine=/Script/MODULENAME.CLASSNAMEWITHOUTPREFIX


Then in the custom UUnrealEdEngine override:



// We want the engine to always run full performance
virtual bool ShouldThrottleCPUUsage() const override
{
   return false;
}


2: The second causes a 5ms sleep when in background and can only be fixed with a custom engine
WindowsPlatformApplicationMisc.cpp
Remove or change the code



FWindowsPlatformApplicationMisc::PumpMessages(bool bFromMainLoop)
{
  ...
#if WITH_EDITOR
   if( HadFocus && !HasFocus )
   {
       // Drop our priority to speed up whatever is in the foreground.
      SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL );
   }
   else if( HasFocus && !HadFocus )
   {
      // Boost our priority back to above normal as initially set in WindowsRunnableThread::CreateInternal.
     SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL );
   }
   if( !HasFocus )
   {
       // Sleep for a bit to not eat up all CPU time.
       FPlatformProcess::Sleep(0.005f);
   }
   HadFocus = HasFocus;

#endif
  ...
}


Usually doesn’t work because of windows and custom drivers for the joypad.
The reason it could work with a 360 controller is the windows drivers for it. The same isn’t necessarily true for an off-brand controller though, or a steam controller in my case.

either way, its mostly a limit imposed by the way the OS works rather than the engine itself.

In other words, should probably be a request for the M$ team rather than the epic team.

Hi, for my research, I am trying to run a racing game (any, for an example, Forza), and another UE custom build game together using same steering wheel and pad. But only one of the game receives the control input. While trying to implement your suggested trick for not in focus joystick signal processing,

  1. I found a large number of DefaultEngine.ini config files. Which one you refer to when you say “UUnrealEdEngine and register it in DefaultEngine.ini config file.”

  2. I may sound naive, but UnrealEdEngine.cpp file is a non writable file. Can you be bit more elaborative how you create a custom UUnrealEdEngine and register to defaultEngin.ini?

Thanks

try unique Wm Input Manager plugin, but your app also have to be configured to run in background