Toggle menu widget with input key binding

Trying to open/close a UMG menu widget with the keyboard and running into trouble.

Setup is starting with a FPS BP template, which has a First Person Character blueprint and Game Mode blueprint.

Someone suggested creating a custom PlayerController blueprint and doing things in there, but it doesn’t seem to work any differently than doing things in the Character blueprint (correct me if wrong).

  1. Create the ‘Menu’ widget blueprint with some clickable buttons, I started from the Pause Menu tutorial but not too important the details since I am focusing on keyboard input for now.
  2. Go to Edit > Project Settings > Engine > Input > Bindings, add ‘ToggleMenu’ ActionMapping and bind to a Key (e.g. M).
  3. In the Character or PlayerController blueprint, add the ‘ToggleMenu’ action node and accomplish the following:
    • Set ‘Input Mode UI Only’: this is to disable player controls such as movement (WASD) or jump (Space)
    • Show mouse
    • Create menu and assign to viewport so it is visible

What I would like now is that the same key binding to ToggleMenu (M) will effect the menu to close:

  • Set ‘Input Mode Game Only’: make so the player can move again
  • Hide mouse
  • Detach menu from viewport so it is hidden

But this doesn’t appear to be possible because:

  • When ‘Input Mode UI Only’ is set, the ‘ToggleMenu’ action in the player controller will no longer fire when the binding key (M) is pressed. So apparently the menu cannot be closed from the Character or PlayerController blueprint.
  • Inside the ‘Menu’ widget blueprint, it is not possible to add the ‘ToggleMenu’ action.

So, how can I trigger the ToggleMenu action to close the Menu using the key that was bound (M)? Some suggestions I have seen is to scan for keyboard input in the Menu widget, but that would not respect the binding (M) set in the Project Settings, correct?

Ok, so the first answer here seems to get me 90% of the way there: Set Input UI Only - Removes Keyboard Input - Programming & Scripting - Epic Developer Community Forums

Don’t use ‘Input Mode UI Only’

Instead:

  • In the widget details, set Input > Stop Action
  • Then, create the ‘Listen for Input Action’ off the Event Construct in the widget BP. The ‘Action Name’ is apparently the InputAction (here ‘ToggleMenu’, it doesn’t auto-fill so needs to be typed correctly but seems to be case-insensitive)
  • From the callback connect a custom event, I guess the name is arbitrary, and add the necessary logic off of that.

It now works! I can open the menu and stop player movement, and close the menu again with the same key binding. (It would also be nice if this was documented somewhere, it seems to be a common problem and a lot of the answers are not so straightforward, such as listening for specific keys in the widget which seems like a bad idea and defeats the purpose of having a single input binding).

Now I said 90% of the way there because: it seems that the menu/widget doesn’t get “focused” when it is opened/added to the viewport with the ToggleMenu. Hovering the mouse over the buttons does not change their appearance, and clicking on the button does not work, it must be clicked once to ‘focus’ the widget, then click again once it has ‘focus’ to click the button. Hopefully it is not too hard to figure this out.

Edit: To solve this last issue, I found to use ‘Set Input Mode Game & UI’ will allow you to set the widget to focus =)

2 Likes

I know it’s been a while, but you’ve just made my day as I’ve been struggling with the same problem:) Thanks for the working solution!