Widget unable to Remove From Parent via keypress

Hello, I am trying to have an in-game menu which pops up with a keypress, and will also close with a keypress.

I have it pop-up no problem via keypress captured from my Controller. And I can close it with a button using the usual remove from parent method.

My problem is the same code on the widget blueprint when originated from a keypress isn’t functioning.

The keypress is captured by overriding the OnKeyDown event, checking for the key (in my case * on numpad), and then calling an Event Dispatcher “Keypress_NumStar” which is bound to an event on the Widget bluprint. I originally just had the close widget nodes in the key down, but was having this issue, so using the Dispatcher to get the code onto the widget blueprint was an attempt (failed) to solve the problem.

I can confirm that the correct code path is occurring. I added the printstring to the exec line on the keypress to be sure that the dispatcher is working correctly and it is.

So with identical nodes on both a button press or key, yet only the button press works, makes me think it’s an issue with focus. Any ideas where to go from here?

Couple more things if relevant:

  • I have flagged “Is Focusable” on the widget.
  • I tried get all of class and it only finds the one instance of the widget. Remove from parent on the array didn’t help.

Any thoughts would be greatly appreciated.
.

1 Like
  1. If you are in a C++ project, create a GameModeObject and set it as the default GameMode
  2. Create a BP_PlayerController from PlayerController class and set it as the default PlayerController
  3. Create an action input event in Project Settings → Input Settings or bind input diretly in the PlayerController
  4. Create an Event Dispatcher in the PlayerController
  5. In the MainMenu widget, bind an event to this Event Dispatcher

PlayerController,
Widget

If you do this in the widget itself, well, widget cannot take input events. It’s not really intended to, and binding the keys directly is kind of like circumventing the system. Now, when the widget is removed, what is there to even run the rest of the code?

Input | Unreal Engine Documentation … it might be a bit over the top for now, but at least as a rule of thumb, try to use the input system instead of directly binding the keys.

As a side note, blueprint event dispatchers are made to enable event calls between different blueprints. In the same blueprint, making a custom event and calling it is enough. There’s a pretty good livestream which explains how communicating between different blueprints works.

1 Like

Great advice JoSf, thanks. I’ll keep my input in the PlayerController. So from PlayerController, I guess I could poll the widgets to see if the InGameMenu was active and if so, remove from parent, and if not, add to view?

Ok, I’m good now, got it working. One final tweak, I had it “set input mode UI only” after adding the menu to the viewport, which was blocking the key. But switching to Game only means losing focus of the widget (I also have mouse buttons on the widget), so “Set input Mode Game and UI” fits the bill. Thanks for your help.

Yeah. You shouldn’t poll though… a reference is all you need, and the engineers at Epic were kind enough to provide an useful boolean for checking the current status of the widget.

1 Like

Perfect, thanks!

Just in case for whom wants to keep using the Set Input Mode UI Only node… (you might need to block input related to in-game logic such as character movement), you can keep your BP as it is and simply override the On Key down event from the desired widget BP, as shown below.

Additionally, by using this method, you can override keys like Tab, which are already predefined to toggle between components within the widget, and make them work according to your own business logic.