Try to use UWidgetBlueprintLibrary::SetInputMode_UIOnlyEx. It’s in #include "Blueprint/WidgetBlueprintLibrary.h"
.
UWidgetBlueprintLibrary::SetInputMode_UIOnlyEx(PlayerControllerToSet, WidgetToFocus, MouseLockMode);
But remember, if you want to return a control of your player pawn, use UWidgetBlueprintLibrary::SetInputMode_GameOnly.
UWidgetBlueprintLibrary::SetInputMode_GameOnly(PlayerControllerToSet);