how do I set user focus to allow menu navigation?

I have a menu UI that I’m trying to allow a gamepad/keyboard to navigate but I seem to be setting the user focus incorrectly.

this is what the inventory widget looks like, the inventory slots are custom widgets arranged in a uniform grid element.

and this is where im setting the user focus in the player controller, i have currently set the focus to be the entire inventory widget. this code occurs after the inventory widget is made visible.

when I play the game and bring up the inventory I cant navigate with the keyboard until I press one of the buttons with the mouse, how do I set it so I can navigate with the keyboard straight away?

Take a look at this video, it is for a gamepad UMG plugin, I haven’t tested it out so I don’t know if it works, but it is worth a try:

thanks but unfortunately its not quite what I was looking for.

I need to know how to set the user focus properly so that the inventory can be navigated as soon as it’s activated by the player.

Did you ever find an answer to this? I’m having the same problem.

My solution was to specify a default menu element to be focused to. When you create the widget you set user focus to that default widget. Additionally you can check on Tick if your menu didn’t lose focus and refocus on default element if it did. Note that your buttons need to be UserWidgets and that it works well only in InputModeUIOnly, I’m struggling to make it work on InputModeGameAndUI. Here is C++ for that:

BaseMenuUserWidget.h

#pragma once

#include "Blueprint/UserWidget.h"
#include "BaseMenuUserWidget.generated.h"

/**
 * 
 */
UCLASS()
class YOURGAME_API UBaseMenuUserWidget : public UUserWidget
{
	GENERATED_BODY()

protected:
		virtual void NativeTick(const FGeometry& MyGeometry, float InDeltaTime) override;
public:

	
	UBaseMenuUserWidget(const FObjectInitializer& ObjectInitializer);

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Init)
	UUserWidget* InitFocusWidget;

	UFUNCTION(BlueprintCallable, Category = Init)
	void SetInitFocusWidget(UUserWidget* InInitFocusWidget);
};

BaseMenuUserWidget.cpp

#include "SurvivalMode.h"
#include "BaseMenuUserWidget.h"

UBaseMenuUserWidget::UBaseMenuUserWidget(const FObjectInitializer & ObjectInitializer) : Super(ObjectInitializer)
{
	bCanEverTick = true;
	bShouldRefocusOnChildrenFocusLost = true;
	//InitFocusWidget->SetKeyboardFocus();
}

void UBaseMenuUserWidget::SetInitFocusWidget(UUserWidget * InInitFocusWidget)
{
	if (InInitFocusWidget) {
		InitFocusWidget = InInitFocusWidget;
		APlayerController* PC = GetOwningPlayer();
		if (PC) {
			InitFocusWidget->SetUserFocus(PC);
		}   
	}
}
void UBaseMenuUserWidget::NativeTick(const FGeometry& MyGeometry, float InDeltaTime)
{
	Super::NativeTick(MyGeometry, InDeltaTime);
	APlayerController* PC = GetOwningPlayer();
	if (PC && InitFocusWidget && !HasUserFocusedDescendants(PC)) {
		InitFocusWidget->SetUserFocus(PC);
	}
}

Later you need to hook SetInitFocusWidget on Construct with a reference to default element.

I also found out on how to check if your widget or anything in it is focused. Just check for bool value of HasAnyUserFocus || HasHasUserFocusedDescendants . If the focus is lost you can set it using SetUserFocus.