# Common UI and Keyboard ONLY, no Mouse

**URL:** <https://forums.unrealengine.com/t/common-ui-and-keyboard-only-no-mouse/1234750>\
**Category:** UI\
**Tags:** unreal-engine, question\
**Created:** [July 20, 2023, 1:38pm UTC](https://forums.unrealengine.com/t/common-ui-and-keyboard-only-no-mouse/1234750 "2023-07-20T13:38:37Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![Ciberus](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ciberus/32/561261_2.png) [@Ciberus](https://forums.unrealengine.com/u/Ciberus)\
**Post date:** [February 11, 2024, 3:45pm UTC](https://forums.unrealengine.com/t/common-ui-and-keyboard-only-no-mouse/1234750/4 "2024-02-11T15:45:55Z")

</div>

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`

```auto
// 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`

```auto
// 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

```

---

_[View the full topic](https://forums.unrealengine.com/t/common-ui-and-keyboard-only-no-mouse/1234750)._
