How can I resolve a focus issue in Slate?

Hey everyone,

hoping there’s someone out there that can point me in the right direction - I’ve been fighting with this for a while now and it’s driving me up a wall.

Here’s the issue:

I have a game, with ESC bound to toggle an in-game menu and at the same time pause the game. That works excellently, and I can hit ESC while the menu is up to toggle the menu again and unpause the game (I’ve got a OnKeyDown event handler in the slate widget taking care of that.

Now here’s the rub: If I click anywhere within the window with the mouse while the game is paused and the menu is up, I seem to lose focus on the widget and the ESC press is no longer caught in the OnKeyDown handler.

I’ve tried hooking up handlers for OnKeyboardFocusLost, OnKeyboardFocusReceived and OnKeyboardFocusChanging but that didn’t get me much further as far as trying to force focus to stay on the slate widget.

Can anyone give me any pointers as to how I should approach this, or maybe even check globally for that keypress instead of just inside this specific widget?

Thanks in advance!

It’s a bit heavy handed but you could try using OnPreviewKeyDown, however if your menu is losing focus I’m not sure that would help.

Hi Jamie, and thanks for taking the time to respond.

Unfortunately I just tried this and it didn’t work - I think I may have found a solution though. It’s kind of ugly and I really should try to find a better solution, but here it is: inside the widget’s Tick I test to see if the in-game menu is open (a flag in my gamestate) and if it is, I simply force focus to the widget by calling FSlateApplication::Get().SetKeyboardFocus(SharedThis(this));

Hopefully someone can give me a pointer to a better solution - to me it seems really brute-force’ish to do it this way and it hurts my professional pride :stuck_out_tongue:

Best regards,
ReqPro.

I’ve had a bit of a play around with this, and you might be able to create a full-screen container for your menu, whose sole job it is to catch any stray mouse clicks.

I’m not sure if this will be compatible with how your menu is set-up, but this is basically a modified version of the Slate HUD sample on the wiki, with the widget that’s added to the viewport vetoing all mouse events.

With this running in my game, I’m not able to interact with the main viewport until I hide the widget by pressing C (I didn’t use Escape since I was testing via PIE).

Just to clarify, with this solution it would be the responsibility of the container widget to handle closing the menu when escape is pressed.

Hi Jamie, thanks again…

Your solution is pretty much exactly what I’d done, except that I didn’t think of intercepting the mouse click inside the widget… (duh)

Thanks for your input!