Lose mouse focus on viewport on lmb click

During PIE, whenever I click left mouse button anywhere in the viewport, mouse is automatically released from viewport, although other controls still work. I then have to rmb to return to the viewport so that my mouse axis will be registered again.
How can I disable or change this behavior?

This can happen if your input mode is Game and UI and there is a widget which is focusable.

There is no widget atm and I tried setting input mode game only and every other solution I could google up, it still insists to do that on left mouse click. Even if I lock the mouse to the viewport, left click still somehow unfocuses it and I lose axes input which I need to rotate camera for example.

Not sure if this is the best solution, but I got around this problem by switching game modes as shown below, not just sticking with one and hoping it works all around. Note that in this example mouse pan is an action binding to a key and this is third person, MMO style. When key is held down, you’re panning camera, when you let go, you get your mouse cursor back. As you have no cursor while panning camera, it shouldn’t matter that input mode is Game Only, you aren’t going to be clicking on any UI elements. Other actual cam control related code omitted from this as unimportant for the solution to original question.

FInputModeGameOnly InputGameOnly;
InputGameOnly.SetConsumeCaptureMouseDown(false);
    
FInputModeGameAndUI InputGameAndUI;
InputGameAndUI.SetLockMouseToViewportBehavior(EMouseLockMode::LockAlways);
InputGameAndUI.SetHideCursorDuringCapture(false);
    
    void ASomeCharacter::MousePanStart()
    {
    	SomeController->SetInputMode(InputGameOnly);
    	SomeController->bShowMouseCursor = false;
    }
    
    void ASomeCharacter::MousePanEnd()
    {
    	SomeController->SetInputMode(InputGameAndUI);
    	SomeController->bShowMouseCursor = true;
    }

That is happening in FSceneViewport::OnMouseButtonUp but only if mouse cursor is visible. Idk why epics decided that is a good idea. But that is how it is coded in the engine.

1 Like