# Close menu with same button as opening

**URL:** <https://forums.unrealengine.com/t/close-menu-with-same-button-as-opening/485710>\
**Category:** UI\
**Tags:** menu-creation, unreal-engine, menu, UI, Blueprint, question\
**Created:** [July 3, 2021, 1:58pm UTC](https://forums.unrealengine.com/t/close-menu-with-same-button-as-opening/485710 "2021-07-03T13:58:33Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![victorialudvigse](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@victorialudvigse](https://forums.unrealengine.com/u/victorialudvigse)\
**Post date:** [July 3, 2021, 1:58pm UTC](https://forums.unrealengine.com/t/close-menu-with-same-button-as-opening/485710/1 "2021-07-03T13:58:33Z")

</div>

Hello! I am new to blueprints and appreciate all help:)

I am currently working on a menu with the setup listed under. The menu opens when you push M on the keyboard. I was wondering if there is a easy way to close the menu with the same button? Thank you so much in advance!

 ![343112-blueprinthelp.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/8/e/58ebf5ac7cd4c40b5c973e5b0fb9e014b82f2b36.png)

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [July 3, 2021, 2:07pm UTC](https://forums.unrealengine.com/t/close-menu-with-same-button-as-opening/485710/2 "2021-07-03T14:07:28Z")

</div>

The most standard way goes like this:

 ![343127-screenshot-3.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/c/6/8c67ab24652d67b987129965e6c3570965911515.png)

The _isMenuVisible_ keeps track of the menu status. You need to add it yourself. There are other ways of keeping track of it but this is generic / versatile.

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [July 3, 2021, 2:08pm UTC](https://forums.unrealengine.com/t/close-menu-with-same-button-as-opening/485710/3 "2021-07-03T14:08:56Z")

</div>

This way you create it only once, and then simply show / hide it instead. You can create the reference to a widget (here NewVar1) by right clicking the _Return Value_ pin → _Promote To Variable_.

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [July 3, 2021, 2:10pm UTC](https://forums.unrealengine.com/t/close-menu-with-same-button-as-opening/485710/4 "2021-07-03T14:10:52Z")

</div>

Technically, you could do this:

 ![343128-screenshot-4.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/b/1/6b1eaf0910e7eae41b28507dceafe2b86481b251.png)

But this will create the menu from scratch every time it’s opened and could be unreliable. Best reference it properly like in the original suggestion.

---

<div class="post-metadata">

**Author:** ![victorialudvigse](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@victorialudvigse](https://forums.unrealengine.com/u/victorialudvigse)\
**Post date:** [July 3, 2021, 2:21pm UTC](https://forums.unrealengine.com/t/close-menu-with-same-button-as-opening/485710/5 "2021-07-03T14:21:03Z")

</div>

thank you! can i ask what is the first node called? the one named SET is menu viable and NOT, and then again is menu visable.

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [July 3, 2021, 3:06pm UTC](https://forums.unrealengine.com/t/close-menu-with-same-button-as-opening/485710/6 "2021-07-03T15:06:13Z")

</div>

As above, it’s a new variable you need to add yourself:

[![Image from Gyazo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/c/e/3cef97f52114ad5c76603087062cd97309b96c36.gif)](https://gyazo.com/f3ce2418bcf33a18ca8c59ce47f9608b)

You can then `Get` (read) it or `Set` (write to) it. You can either drag & drop it into the graph or hold Ctrl / Alt as you drag to evoke get / set.

All the good stuff about variables:

> **[Blueprint Variables](https://docs.unrealengine.com/4.26/en-US/ProgrammingAndScripting/Blueprints/UserGuide/Variables/)**
>
> Properties that hold a value or reference an Object or Actor in the world.

Just in case.

* * *

Alternatively, you could do it like this:

![343133-screenshot-10.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/b/7/eb75ad836f76c80991f5b4e286ffd7aecb2768df.png)

---

<div class="post-metadata">

**Author:** ![victorialudvigse](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@victorialudvigse](https://forums.unrealengine.com/u/victorialudvigse)\
**Post date:** [July 3, 2021, 4:49pm UTC](https://forums.unrealengine.com/t/close-menu-with-same-button-as-opening/485710/7 "2021-07-03T16:49:42Z")

</div>

thank you so much for your respons! i really appreciate it 😃 i tried the second version, and this is how my blueprint looks like now. i will try your first method now. it did not work with the second, i dont think i did it right… the menu does not close. thank you again!

 ![343117-blueprinthelp2.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/5/2/a529050e8ec663f2a05139824c6bae97be8df38f.png)

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [July 3, 2021, 5:25pm UTC](https://forums.unrealengine.com/t/close-menu-with-same-button-as-opening/485710/8 "2021-07-03T17:25:54Z")

</div>

This is not working because you’re cutting Player Controller from input by setting UI mode only.

---

<div class="post-metadata">

**Author:** ![victorialudvigse](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@victorialudvigse](https://forums.unrealengine.com/u/victorialudvigse)\
**Post date:** [July 4, 2021, 6:06am UTC](https://forums.unrealengine.com/t/close-menu-with-same-button-as-opening/485710/9 "2021-07-04T06:06:02Z")

</div>

ok thank you again! can i just cut the player controller? sorry for the stupid questions… ☹

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [July 4, 2021, 8:04am UTC](https://forums.unrealengine.com/t/close-menu-with-same-button-as-opening/485710/10 "2021-07-04T08:04:02Z")

</div>

Not stupid at all, actually. It’s just an input processing thing.

`Set Input Mode UI Only` prevents the Player Controller from processing input - pressing M while this mode is active does nothing, the PC ignores it, as instructed. The input is processed in the widget instead since we’ve given it to the UI exclusively - `Set Input Mode UI Only`

You can have it processed by both (widgets go first, though) with `Set Input Mode Game and UI` node but if you’re making a first person shooter kind of game (we don’t know it here) you may not want to do this.

In order to make the same key work seamlessly (the way _I think_ you want it to work,) you’ll need the following:

In the player (or wherever you press M to open the menu):

 ![343140-screenshot-1.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/7/0/2706e4aac4c98d07c182441c755cd3b6e5afdf24.png)

- the above either creates or shows the widget, it does not close it
- the below is processed in the widget - this closes the menu:

 ![343185-screenshot-3.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/1/1/51103a4ff25277cb943bb8f3bdfe3681856e1f58.png)

- the override is here:

 ![343183-keydown.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/f/4/df4a69e6877473650700e428b5d9cc6333ea532e.png)

- and you need to make the widget keyboard focusable:

![343184-screenshot-2.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/a/e/bae3a5a9f8dc86290925605d00c72dc346cf5298.png)

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [July 4, 2021, 8:08am UTC](https://forums.unrealengine.com/t/close-menu-with-same-button-as-opening/485710/11 "2021-07-04T08:08:51Z")

</div>

The above would work in an FPS where you _do want_ input exclusivity:

[![Image from Gyazo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/6/c/56c1f5b0d45d918bbc8215dbdc2e2ae3bd8eb6eb.gif)](https://gyazo.com/574aac4f1edefbabae8d0cd0190b88de)

---

<div class="post-metadata">

**Author:** ![victorialudvigse](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@victorialudvigse](https://forums.unrealengine.com/u/victorialudvigse)\
**Post date:** [July 4, 2021, 9:49am UTC](https://forums.unrealengine.com/t/close-menu-with-same-button-as-opening/485710/12 "2021-07-04T09:49:35Z")

</div>

thank you so much for all your answers! i will try this out now 😃

---

<div class="post-metadata">

**Author:** ![AllGamesSuck](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AllGamesSuck](https://forums.unrealengine.com/u/AllGamesSuck)\
**Post date:** [February 20, 2024, 4:14pm UTC](https://forums.unrealengine.com/t/close-menu-with-same-button-as-opening/485710/13 "2024-02-20T16:14:21Z")

</div>

> [@Everynone](#):
>
> Technically, you could do this:
> 
> ![343128-screenshot-4.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/b/1/6b1eaf0910e7eae41b28507dceafe2b86481b251.png)
> 
> But this will create the menu from scratch every time it’s opened and could be unreliable.

Why is this unreliable? Removing widget from parent removes from memory, right? Isn’t it better that way?

---

<div class="post-metadata">

**Author:** ![AllGamesSuck](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AllGamesSuck](https://forums.unrealengine.com/u/AllGamesSuck)\
**Post date:** [February 20, 2024, 5:15pm UTC](https://forums.unrealengine.com/t/close-menu-with-same-button-as-opening/485710/14 "2024-02-20T17:15:12Z")

</div>

> [@Everynone](#):
>
> - the below is processed in the widget - this closes the menu:
> 
> ![343185-screenshot-3.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/1/1/51103a4ff25277cb943bb8f3bdfe3681856e1f58.png)

This doesn’t work anymore if you click anywhere outside the widget, hence it looses focus.  
Why aren’t widgets accepting key input?

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [February 20, 2024, 5:56pm UTC](https://forums.unrealengine.com/t/close-menu-with-same-button-as-opening/485710/15 "2024-02-20T17:56:26Z")

</div>

How long do you think this reference is valid? 5s, 60s until GC kicks in, 5 minutes, entire session? And what if something else needs to access it, it cannot.

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [February 20, 2024, 5:57pm UTC](https://forums.unrealengine.com/t/close-menu-with-same-button-as-opening/485710/16 "2024-02-20T17:57:33Z")

</div>

Did you forget to set the Input Mode to UI only? The focus can only go to a widget…

You can always onFocusLost → Focus.

And keyboard Focus is a thing, too.

---

<div class="post-metadata">

**Author:** ![AllGamesSuck](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AllGamesSuck](https://forums.unrealengine.com/u/AllGamesSuck)\
**Post date:** [February 20, 2024, 6:01pm UTC](https://forums.unrealengine.com/t/close-menu-with-same-button-as-opening/485710/17 "2024-02-20T18:01:35Z")

</div>

Yep, UI only set in Player Controller. As soon as I click outside any visible component of the widget, it doesn’t fire the ‘onKeyDown’ function anymore. I assume it looses focus?

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [February 20, 2024, 6:06pm UTC](https://forums.unrealengine.com/t/close-menu-with-same-button-as-opening/485710/18 "2024-02-20T18:06:43Z")

</div>

Refocus then, there is an override for that.

---

<div class="post-metadata">

**Author:** ![AllGamesSuck](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AllGamesSuck](https://forums.unrealengine.com/u/AllGamesSuck)\
**Post date:** [February 20, 2024, 7:12pm UTC](https://forums.unrealengine.com/t/close-menu-with-same-button-as-opening/485710/19 "2024-02-20T19:12:33Z")

</div>

Well, just used onFocusLost → SetFocus (self). This is on my main menu. All good except the Quit Game button doesn’t work anymore, lol.  
Using some print strings, I can see that, when I press Quit, onFocusLost fires.  
The onKeyDown for closing the menu still works though.

Edit:  
Apparently, everytime a child component of the widget gets focused (clicked buttons, in my case), the widget itself loses focus. Why is that useful is a mistery?

All you have to do is uncheck this:  
 ![Capture (4)](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/4/a/d4aa5ddfa5fb3340e6fd1bdc08568cdc3eac756b.png)  
on every child component. But I assume this will throw any keyboard/controller navigation of the UI out the window.

Another few good hours lost on figuring out something that should be trivial. I hope there’s good reasons for things to be designed like this.

Thanks for the help, once again.

---

<div class="post-metadata">

**Author:** ![AllGamesSuck](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AllGamesSuck](https://forums.unrealengine.com/u/AllGamesSuck)\
**Post date:** [February 21, 2024, 11:57pm UTC](https://forums.unrealengine.com/t/close-menu-with-same-button-as-opening/485710/20 "2024-02-21T23:57:12Z")

</div>

Spoke too soon. Forcing focus back on the main widget prevents ComboBox from working. And who knows how many other things, unfortunately.
