Bug with mouse control of game in editor?

I’m having issue with mouse control/Events in my game.

I made a project using the C++ FPS template.
I bound a function to an input wich toggle between FPS control and mouse control:

void AMyHUD::ToggleFreeCursor()
{
	if (!PlayerController) return;

	IsCursorFree = !IsCursorFree;
	PlayerController->SetIgnoreLookInput(IsCursorFree);
	PlayerController->bShowMouseCursor = IsCursorFree;
}

It works very well only if I don’t click between tow toggles.
I launch the game, click in the viewport to take control (I see the notification “Click Shift+F1 for Mouse Cursor”), I can move the camera, I press my toggle key, the cursor appear and the camera doesn’t move anymore, I toggle again: the cursor disappear and I move the camera.

But,

If after the first toggle I click (anywhere), I see a “Click Shift+F1 for Mouse Cursor” notification and immediatly after “Click for Mouse control”, and when I toggle again I need to click again to be able to move the camera. (and when I make this click, only the notification “Click Shift+F1 for Mouse Cursor” appear).

Why do the game doesn’t keep the Mouse Control when I click while SetIgnoreLookInput and bShowMouseCursor are true?

It is kind of a long story and one we are aware of and have plans to fix, but it is tangled up with a few other issues.

The medium legnth version of the story (there isn’t really a short version) is that the Slate mechanism for capturing the mouse to window which allows the mouse free look to work only responds to mouse input events and in-game input events handled by the input components is deferred to later in the frame, so between it being a keyboard toggle and we don’t actually send back the capture message directly from the key press, Slate doesn’t actually know the game viewport wants to capture the mouse cursor again.

As I said we do have a plan to fix it, but it isn’t trivial and tied up with some other refactoring work, so I can’t say for sure when it is going to happen.

Apologies for that.

Still an issue for me in engine version 4.11.2.

Is there a workaround to this? Cant figure one out myself, kinda stuck on this issue.

Now I feel really stupid I didn’t look just a bit longer for the solution, as a reference to others who might come across this issue, this is how I solved it.
Big thanks to Fen from this thread.