Clicking changes focus from widget to viewport

Hello,

I’m trying to create a menu that can be navigated through keyboard/controller only.

Right now, the issue I’m running into is the menu widget losses focus if there’s a click input from the mouse. It doesn’t seem to matter if the mouse has been captured, hidden, or if an input mode is specified. Steps to reproduce:

Create empty level

Create widget blueprint

Add button to widget

Display widget in level (as per LevelBlueprint.png the button will have focus from the keyboard)

Launch game in New Editor Window

Following these steps gets the button focused and responds to keyboard input, which can be verified by using the Widget Reflector (Window → Developer Tools → Widget Reflector, then check “Show Focus”) but once the viewport has been clicked the keyboard will lose focus (BeforeClick.png and AfterClick.png).

I’ve tried just about every solution I can find to no avail. The closest I’ve come to resolving the issue was through the very similar post [bug] losing focus when clicking in level view - UI - Unreal Engine Forums

The conclusion in that answer section was:

“I made a blueprint exposed function that looksup the currently focused widget and sets a focus reply event. I called it keepfocushandled which returns an event reply for the mouse down event. This works fine, although this solves my mouse click issues i found thst keyboard navigation can also cause the viewport to receive focus. It would be great if you guys would show an umg example which supports keyboard, gamepad and mouse navigation.”

I’m uncertain as to how you can look up the currently focused widget however. I’m at a loss.

Any help would be greatly appreciated!

I am having a similar problem and I have not found a resolution to this yet. Anyone out there have an idea?

Got same problem, my most blunt solution so far is:

266850-keepfocus.png

Getting the player controller and setting focus on tick seems pretty expensive.

I have the exact same problem… Please, we need help !!! lol

My menu support Keyboard, Mouse an gamepad very well.

I copy the text of my answer I wrote here : https://answers.unrealengine.com/questions/331759/umg-focus-is-lost-when-clicking-on-visible-widgets.html?childToView=906845#answer-906845

"

I give my solution, I hope it will help you (and the other people who come to this forum and were looking for the solution like me, 30 minutes ago, before I found this little trick! ^^)

1- record the focused button in the Event Tick

2- Create a giant, invisible button, without pressed padding, which contains the entire menu.

3- When clicking on this giant invisible button (OnPress, not OnClick), set the focus to the current focused button

It works perfectly for me! ^^
Final result: https://streamable.com/kwm4c

Tip: you can remove the default dotted frame around the button by disabling this feature in project settings:

"

I was losing focus when I looted an item from my loot menu after killing an enemy. My loot menu consist of a grid which populates buttons as children. I determined clicking the button is what was causing me to lose focus on the widget (because it lost focus each time I clicked it).

Here’s how I fixed this problem. I added a function to the interface I was already using. If you’re not using an interface, create one and add the interface to the widget that is losing focus. For me, it was my loot menu that would lose focus every time I clicked the button to loot an item. The focus would return to my HUD but the menu would remain, and items were still available for looting. Add necessary inputs to the interface, for me, I need the child index for the array so it’s set to the correct button.

Create the function in your blueprint interface.

Add the interface event to the widget that loses focus.

On the button causing me to lose focus, I went to the event on clicked (root cause) and added get all widgets with the interface (selected the correct interface in the class drop down and checked top level only) to the end. Then dragged out the array pin and call the event.

Hope this helps.