Focus Issue when adding UMG Widget in code.

In my player controller Blueprint i create a couple of widgets and add them to the viewport.
For the Inventory Widget the visibilaty is set to hidden, and when player presses the “I” button.
It is set to Visible, Show Mouse Cursor is set to true, and IgnoreMouseLook is set to true.
And this works great it catches all the events and reponds to mouse clicks and so 4th.

Doing the same in code and the widget dose not respond to any input.
Nothing happens and its like there is no functionality for it.

Its added like this in BeginPlay() of my Player Controller.

if (IsLocalPlayerController())
{
	// Create Inventory User WIdget UI
	if (InventoryUIClass) // Check the selected UI class is not NULL
	{
		if (!InventoryWidget) // If the widget is not created and == NULL
		{
			InventoryWidget = CreateWidget<UInventoryUserWidget>(this, InventoryUIClass); // Create Widget
			if (InventoryWidget)
			{
				InventoryWidget->AddToViewport(); // Add it to the viewport so the Construct() method in the UUSerwidget is run.
				InventoryWidget->SetVisibility(ESlateVisibility::Hidden); // Set it to hidden so its not open on spawn.
			}
		}
	}

And when player Toggels Inventory.

if (bShowingInvetory)
	bShowingInvetory = false;
else
	bShowingInvetory = true;

OnToggleInventory.Broadcast(bShowingInvetory);

if (InventoryWidget)
{
	if (bShowingInvetory)
	{
		InventoryWidget->SetVisibility(ESlateVisibility::Visible);
		bShowMouseCursor = bShowingInvetory;
		SetIgnoreLookInput(bShowingInvetory);
	}
	else {
		InventoryWidget->SetVisibility(ESlateVisibility::Hidden);
		bShowMouseCursor = bShowingInvetory;
		SetIgnoreLookInput(bShowingInvetory);
	}
}

Way do i not get the same result?
Found someone else on AnswerHub with a similar problem and answer sugested this.

		FInputModeGameAndUI Mode;
		Mode.SetHideCursorDuringCapture(false);
		Mode.SetWidgetToFocus(InventoryWidget->GetCachedWidget());
		SetInputMode(Mode);

It did not solve my issue tough.
Its clearly the exact same approche, but it somehow fails when done in code.

Thanks for any help.

I will report this as a bug.
Creating the widget in Blueprints and then setting the C++ member works just fine.
Doing the exact same thing in code and the UMG widget fails to catch any input.

Hey again, what exactly is capturing the input on your widget? Is it generic things like buttons? Or do you have events/code based on the key events on your user widget class?

I got really frustrated by the focus and input capturing system in slate/umg so I ended up restricting us to only creating user widgets of our own class and wrote a focus system on our player controller. I check every trick to make sure our top widget has the focus.

Anyway is my function I use to set the focus back atm:

void ASubmergedPlayerController::ResetFocusToTopWidget()
{
	USubmergedUserWidget* FocusWidget = GetTopFocusWidget();

	if (FocusWidget)
	{
		FInputModeGameAndUI Mode;
		Mode.SetWidgetToFocus(FocusWidget->GetDesiredFocusWidget());
		SetInputMode(Mode);
		FSlateApplication::Get().SetKeyboardFocus(FocusWidget->GetDesiredFocusWidget(), EKeyboardFocusCause::SetDirectly);
	}
	else
	{
		FInputModeGameOnly GameMode;
		SetInputMode(GameMode);
		FSlateApplication::Get().SetFocusToGameViewport();
	}
}

The only difference is that I force the keyboard focus as well now.

I capture input for the keyboard/controller in the user widget based classes like so:

virtual FEventReply OnKeyDown_Implementation(FGeometry MyGeometry, FKeyboardEvent InKeyboardEvent) override;

FEventReply USubmergedUserWidget::OnKeyDown_Implementation(FGeometry MyGeometry, FKeyboardEvent InKeyboardEvent)
{
	if (bLockControls)
	{
		FEventReply Reply;
		Reply.NativeReply = FReply::Handled();
		return Reply;
	}
	else if (InKeyboardEvent.GetKey() == EKeys::Escape)
	{
		RequestClose();

		FEventReply Reply;
		Reply.NativeReply = FReply::Handled();
		return Reply;
	}
	else
	{
		return Super::OnKeyDown_Implementation(MyGeometry, InKeyboardEvent);
	}
}

I also added my own special slate widget which stretches over the whole screen and captures any mouse clicks and keeps focus on my current top screen. This works well for our purposes.

Hello and thanks for the great reply.
I found a workaround for my problem and it was.

To create the widget in Blueprints and set the C++ Member for it in BP.
I failed to figuer out way none of the widgets can cathe events/ interact with the Widget when its created nativly.