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)
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 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.
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! ^^)
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.