OnKeyDown - Issue if changing InputMode

Hi,

If during the event “OnKeyDown”, you are moving from UIInput to GameInput, the current Key pressed is handled even if you set the output to “handled”

Steps

  • Create a Widget
  • Add a “KeyDown” Override function
  • For any key pressed call PlayerControler->SetInputMode(GameOnly)
  • Set the output to handled
  • In your PlayerController, for Any key that you pressed, create the widget, add it to the viewport and set inputmode to “UIOnly”

The goal is: The playercontroller will get a key (like from a action binding), create the widget (see it as a ingame menu), add it to the viewport and set the inputmode to be UI only so it won’t react to any key mapping anymore.
On the widget when a key is pressed (like Echap) you want the menu to close, so in the key Event, you call the function that will remove this widget from the viewport and set the Inputmode to GameOnly.

What you will observe is that the menu will show off and show On again.
In fact, the “Escape” key is taken into account by the Widget (perfect) but as you are moving back to “GameOnly” and even if you set the KeyEvent to “Handled” the player controller will receive the KeyMapping and open your Menu Widget again.

Thanks!

The workaround is to add a “delay” to the call to “show off” your menu widget.

Hi,

As you understood the Goal is to Open / Close an InGame Menu with “Echap”

The OpenMenu function is an ActionBinding on the PlayerController that call a Hud Function that will create the UMG Widget and set the InputMode to UIOnly.

Now I want to call back the HudFunction to remove the widget from the viewport and put the InputMode back to GameOnly.

In my widget, I’m using KeyDown event to handle the call to the HudFunction when I’m finding that the Echap Key has been pressed.

KeyEvent-> Echap Pressed? → If true called a BPCallable function of my ingameMenu that will call the Hud function to remove it from viewport and set the InputMode back to Game.-> set the event as “Handled”

If I’m doing this, the menu will be remove from the screen and recreated back again.

What I found is that once, the HUD function remove the widget and set the input mode back to Game, The playercontroller ActionMapping will be call so the menu is “recreated”.

My workaround was to call a custom event in the widget with a delay function that will call the “Close Menu function” after 0.2sec. So the KeyEvent is handled and the PC ActionMapping is not called.

I hope it’s clearer. If not, I will try to add more information later on.

Thanks,

Hello,

Could you explain more clearly what it is that you are trying to accomplish and how the results are at an inverse to what is expected. It sounds like you are trying to use a key press to bring up a menu and then to have that same key remove that menu from the viewport. Is that correct? If so, I would able to do this without any issues. Could you provide screen shots of any blueprints that may be involved?

Thank you for the additional information. Would it be possible for you to provide a few screen shots of any blueprints that are involved with this issue? This way I will be able to more accurately answer your question.

Hello,

We have not heard back from you in a few days, so we are marking this post as Resolved for tracking purposes. If you are still experiencing the issue you reported, please respond to this message with additional information and we will offer further assistance.

Thank you.

SOrry for the delay.

Here are the

You can see how I manage my key event and my custom event.
Everything else is in C++ and is described in my preview post. I hope it will be easier for you to repro the issue.

In a normal case, I should have called “OnShowOff” directly in the keydown event.

thanks,

After running a few tests I did not see anything that was not working as intended. I have provided an example below of a working menu that toggles on and off while also handling the input mode. Let me know if this helps and if this would be a viable method for you.

Example:

This was done in my player controller. This simply opens the menu when I press ‘Q’.

Here is my test menu. Please note that I set the canvas panel to visible. This is so that when I click around the screen the widget will not loose focus.

This is the widget that was just opened. As you can see it sets the input mode to UI only when it is created. It also tells the mouse to show up.

This is my OnKeyDown override. Here I check to see what key the user is pressing and if it is the ‘Q’ key and that is true; I set the input mode, remove the cursor, and then remove the widget itself.

Hi,
Thanks for the update. I dig again in this issue and I found the root cause. In fact my ActionMapping was set “on key Released” and not “on Key pressed” so this is why I had this issue. I’m sorry for the time you spent on this, but your sample help me to track down the issue.

Thanks,

I am happy to hear that your issue has been resolved. I will be converting your last comment to an answer. If this issue returns please feel free to reopen this thread with additional information.

Make it a great day