Text Box Loosing Focus When Pressing ESC Key

Hi,

I’m working on a chat system. The idea is that when a player presses the chat button, the mouse cursor is hidden, a text box appears, the focus is set to UI Only and all that the player can do from there is input text into the text box. Once the text is committed, the text box is hidden again, and the focus is reverted to Game Only.

So, I’ve got a widget blueprint that contains a Canvas Panel and a Text Box as its child. Everything works fine until I press the ESC key, after which the text box loses focus. From what I understand, the focus is set to the Canvas Panel. :thinking: To get the text box to focus again I have to press the TAB key.

I tried to fix the issue by adding a OnKeyDown override in the widget’s blueprint, which would return focus to the text box when the ESC key is pressed. But that only works when I press the ESC key twice.

From what I understand, what I’m getting is:

ESC Key (first press): Switch focus from Text Box to Canvas Panel.
ESC Key (second press): Switch focus to Player Controller and run whatever is in OnKeyDown.

I’m at my wit’s end, does anyone know what to do? :exploding_head:

What do you want to happen when the user presses ESC?

You could use the “On Cleared” Commit Method from the “On Text Committed” event, which only fires when the user removes their Keyboard Focus by ESC or a similiar action.

Hope that helps you out somehow.
Also looked into using the PlayerController as a sort of InputDetector and the fire a EventDispatcher as soon as the PlayerController detects the Escape Key pressed.

But that didn’t work, because it focuses the Canvas Panel first :man_shrugging:

The “On Cleared” method worked BUT, before you can set focus to the text box again, you have to set focus to something else, such as the background first, and then set focus to the text box. Otherwise, it won’t work.

2 Likes

Wow, this is bug level behavior from the engine. I can confirm after user presses ESC and the ‘On Cleared’ method of commit event occurs… you must do something hacky like call “Set Focus to Game Viewport” before you can set focus back to your textbox. Yikes.