Alt-Tabbing out of one viewport and then back in does not clear/reset the ALT key or keyboard input

My Solution I have resolved this for time being by running a timer every 0.333 seconds that does the following check

//is viewport no longer foreground in OS ?
if (!VictoryViewport->IsForegroundWindow())
{
	//reset player input
	if(PlayerInput) PlayerInput->FlushPressedKeys();
}

This does work and solves the issue, and answers my question in the main post below, which is what does the ~ / console key do.

It is however still a workaround, please examine this issue and see if you can implement a

Complete Solution

Whatever function gets called when user Alt-Tab’s out of a viewport (not in editor, single instance only),
you could have that function also call something like

if(PlayerInput) PlayerInput->FlushPressedKeys();

(I crashed my game by not including the if(PlayerInput) in multiplayer :slight_smile: )



Update 2: Yes you can reproduce this in single-player instance, not in editor, using steps below, and actually whatever keys you were pressing when you alt-tab out, if you press those same keys it does finally clear the input, but pressing other keys does not, so character will keep running in whatever direction it was going when you alt-tab out until you press the same input again.

ex: while holding A to run sideways, press ALT-Tab and character will keep going even when you tab back in, till you press A, other keys dont stop it.

the point:

The ALT key is also not being cleared till you press it again, which is not exactly intuitive to the person alt-tabbing or convenient for my keybinding system which relies on the value of ALT key to be correct at all times .


Update: You should be able to reproduce this with my steps below in a single player instance, again not running in editor, just the game itself.


Reproducing:

I can reliably reproduce this issue in current build by

  1. having two game instances / viewports open, not running editor, and having the games connected via 127.0.0.1
  2. have 1 character start running pressing A or D
  3. while holding down A or D, press ALT TAB to go to other game instance
  4. first character will keep running, the ALT command will not be cleared

It doesnt happen every time unless you press all the keys just right, but when it does happen it is game-breaking for me due to my custom keybinding system which relies on the ALT key being cleared,

as well as the fact that when I tab back in I cant stop the character from running sideways till I press ~ / Console which may not be available in shipping builds


Dear Friends at Epic,

I am loving UE4, thanks so much for making it!


Situation 1

I have two viewports open for testing multiplayer, I am not in the editor, I am using port 127.0.0.1

Often times if I am having 1 character running with WASD and then I tab over to the other window, the first character will keep running indefinitely as the input does not get cleared when tabbing out

that’s fine, but when I tab back in, I cannot get the character to stop running

The character has become unresponsive to new WASD input!


unless I press ~ / console key, which somehow clears the input


Situation 2

I have a custom keybinding system that checks whether LeftControl, LeftAlt and Leftshift are down (using IsInputKeyDown from PC class)

When I tab between the two viewports, the LeftControl, LeftAlt and Leftshift flags seem to get all mussed up, so that when I tab back in, the game thinks those keys are being held down,


resuming normal key pressed and even pressing CTRL ALT or SHIFT does not fix this behavior, and my keybinding system becomes broken because it thinks those keys are being held down when they are not and does not allow my key press to register (because it thinks I am pressing CTRL T for example, when I am just pressing T)

until I press ~ / console key

Then the CTRL SHIFT ALT flags get cleared and if I press just T (no ctrl or shift or alt), that is what my keybinding system gets and allows the command to pass


My Main Question

so my question is

what is the ~ / console key doing to clear the input?

Because I can manually detect when the viewport becomes foreground window and call this clearInput myself, or whatever function it is

In the long run it’d be nice if this issue could be addressed, as in the full game the console may not be available and I need some way to clear input or when tabbing back in the game engine should clear the input automatically


System Specs

I’m using Windows 7 64 bit


Thanks so much!

Rama

Hi Rama,

I have reproduced and reported the issue. I am glad you have found a solution. I will let you know if there are any developments.

Cheers!

-Stephen

yay!

Great to hear from you Stephen!

:slight_smile:

Rama

Awesome question and solution! Even still in 4.10.2, under Windows, this problem persists, and Rama’s solution works perfectly.

So - I haven’t seen any development on this issue and it appears to still exist in Engine Version 4.18. I can’t find any other good answers on the internet so I’m posting some code here you can more easily plop into the core engine code.

If you want to implement this without Rama’s code base you can just make a few small changes in PlayerController and recompile and be good to go. Can even implement the timer in blueprints if you want to after these changes.

1). Add a method to player controller for IsForegroundWindow as the FViewport classes aren’t blueprint visible.

 //BEGIN SYMMETRIC MOD - JB
//Be able to get IsForegroundWindow from the player controller class
bool APlayerController::IsForegroundWindow()
{
	ULocalPlayer* LocalPlayer = Cast<ULocalPlayer>(Player);
	if (LocalPlayer)
	{
		UGameViewportClient* ViewportClient = LocalPlayer->ViewportClient;
		if (ViewportClient)
		{
			FViewport* Viewport = ViewportClient->Viewport;
			if (Viewport)
			{
				return Viewport->IsForegroundWindow();
			}
		}
	}
	return false;
}
//END SYMMETRIC MOD - JB

2). Change the .h file

//BEGIN SYMMETRIC MOD (Make this function blueprint callable for fixing mouse capture errors)
UFUNCTION(BlueprintCallable, Category="Input")
virtual void FlushPressedKeys();
UFUNCTION(BlueprintCallable, Category = "Input")
bool IsForegroundWindow();
//END SYMMETRIC MOD

3). Go use your new blueprint visible functions to write the same timer loop as Rama has running natively.

Enjoy - and thank you Rama for all of your amazing work - I’ve seen a lot of your posts and they have been very very helpful to me.

1 Like

Hey y’all,

I ran into this issue too (on 4.24). I think I found the root cause. When alt-tabbing, the viewport loses focus and calls UPlayerInput::FlushPressedKeys(). However, there is an issue in that function, it only clears already pressed keys, but not the pending ones.

Here’s how I fixed it:
Find this block of code:

const FKeyState& KeyState = It.Value();
if (KeyState.bDown)
{
	PressedKeys.Add(It.Key());
}

and add this right under it:

const int32 PressDelta = KeyState.EventAccumulator[IE_Pressed].Num() - KeyState.EventAccumulator[IE_Released].Num();
for (int32 i = 0; i < PressDelta; ++i)
{
	PressedKeys.Add(It.Key());
}