Common UI and Keyboard ONLY, no Mouse

Btw in case anyone wants to support mouse also, I found a solution and verified it works in production

Mostly when you click outside any widget, Viewport receives focus thats not what we want, to prevent such behavior:

  1. Create button class inherited from UCommonButtonBase with NativeOnFocusChanging
// UBBaseCommonButton.cpp 
class BOGATYR_API UBBaseCommonButton : public UCommonButtonBase, public IUserObjectListEntry
{
	GENERATED_BODY()

public:
#if PLATFORM_DESKTOP || WITH_EDITOR
    virtual void NativeOnFocusChanging(const FWeakWidgetPath& PreviousFocusPath, const FWidgetPath& NewWidgetPath, const FFocusEvent& InFocusEvent) override;
#endif
};
  1. in NativeOnFocusChanging check that FFocusEvent was caused by mouse and new focused widget type is SViewport or for editor - SPIEViewport and prevent such event by focusing back button, not ideal but good solution in my case
    .h
// UBBaseCommonButton.h

#if PLATFORM_DESKTOP || WITH_EDITOR
void UBBaseCommonButton::NativeOnFocusChanging(const FWeakWidgetPath& PreviousFocusPath, const FWidgetPath& NewWidgetPath, const FFocusEvent& InFocusEvent)
{
    TSharedRef<SWidget> NewWidget = NewWidgetPath.GetLastWidget();
    TWeakPtr<SWidget> PreviousWidget = PreviousFocusPath.GetLastWidget();
    FName WidgetType = NewWidget.Get().GetType();

    if (InFocusEvent.GetCause() == EFocusCause::Mouse
        && (WidgetType == FName("SPIEViewport") || WidgetType == FName("SViewport")))
    {
        SetKeyboardFocus();
        return;
    }
    Super::NativeOnFocusChanging(PreviousFocusPath, NewWidgetPath, InFocusEvent);
}
#endif
4 Likes