Keyboard input in userwidget

Hello

I have been trying to add keyboard input to one of my userwidgets without any succes. There seems to be nothing about it online either so my question is how do I have one input doing different things depending on which userwidget I am in?

Thanks for your help

Hi Dennis Von Hedberg,

The keyboard input can be set in your player controller. When you activate the key have it check which widget is currently set to viewport, and start the functions that you have set up in the widget. That should give you the control you are looking for from keyboard to UMG.

I am not sure how i should do that and if you understood what i meant. I want this line of functions inside of my userwidget.

I found that way too, but do you know if it have any support from the key you setup inside of your action maping and axis maping. And does this way override the function from the same key inside of your player character?

Hi!
I’ve managed to find a way to capture keys in the widget blueprint. Take a look here. I don’t know if this can help you.
In my case I look if the Player Controller has pressed “M” key and then I remove the widget. but since this is inside a widget blueprint, you can do whatever you want :wink:

3 Likes

Thank you so much! It working, I’ve been searching for 2 days after this! :slight_smile:

THANK YOU FOR THIS AWESOME WAY TO DO THIS

Hey,

I know this is an old post, but in case anyone else needs help getting a widget to register key presses I’ve found two ways:

1 - Using “Listen for Input Action”, whenever you press a key associated with that action mapping it will trigger the “callback” event.

You have to “Set Input Mode Game And UI” for this to work.

“Consume” makes sure that this widget takes priority on what listens to that key press, e.g. so that when you press “jump” it isn’t calling jump events on your character and in your widget, just the widget.

With this method you can change the keys associated with an action mapping and that change will be reflected in all widgets that you use this method with.

If an action mapping isn’t specifically being “listened” to in the widget, your pawn will still receive that input.

This method doesn’t seem to work if the game is paused, because the pawn will then receive no input.

2 - Overriding the “OnKeyDown” function to trigger a custom event that will do different actions based on what key was pressed.

I set the input mode to UI only.

Make sure to go to the widget options under “interaction” and set “is Focusable” to true.

With this method you can do it while the game is paused and the pawn won’t be receiving inputs.

If you want to add another key to trigger an event you will have to manually add this change to all your widgets that you use this method with (I haven’t figured out a way of avoiding that).

10 Likes

Hi Leone. I just wanted to say thanks as this issue was giving me a bad headache. I used your first solution and it has solved my problem completely. So glad I found this post. I’m almost done with my game and I couldn’t believe this little thing was causing me more of a headache than everything else in the game mainly because my game consists of mainly widgets and only 2 blueprints… Thanks again!

Another way to do that, in a paused game (Easy Way) is to simulate a paused game using Set Time Dilatation = 0. Doing that the system still get the character inputs but the game is “paused”.

This works very well for me.

1 Like

Hi i would like to ask about this, i would like to use this method via pause menu. i already have my escape key for pause menu, but it seems that when i try to use this under the widget that needs to be closed, the event tick is functioning itself the way that i didnt intend to. what i mean is, it supposed to work when i want to close the pause menu using the escape key

It’s generally a very bad idea to branch logic against display strings, as they can change if the language changes.

If you have a “Key” struct (eg, from calling “Get Key” on a key event), you can use its “Equals” node to compare it against the value of another “Key” struct. That is safer (and faster) than comparing display strings.

1 Like

Second solution works the best

I set my widget up with no game pause and not in ui only input mode but set a bool to true when it is opened to stop inputs from doing other things besides what I want them to do in the ui. you can actually just throw an enhanced input action in the widget event graph once you have them set up for your character.

also the on clicked below that is the actual button in the widget that would do the same thing. both the widget button and my input trigger that event.

hello am 8 years from future, after thousands of years figuring out how to get a widget to do input, i have figured out the following (ue5.3):

  • widgets that have listen for input, input event, or onKeyDown input only work for the most recently created widget. That means only 1 widget of a class can have input at a time…
  • focused widgets along side game&UI input mode, cause player movement keys to stick…
  • attempts at a c++ solution will not work because HUDhitboxes do not have any reference to UMG

your solution is the only solution that works… so sad to see UI inputs are still messy, AND to make matters worse this solution doesn’t even work with the new input system!!!

still works though!

2 Likes

Hello, if you have input config set to game or game and menu, then you can normally listen to input events like AnyKey
If you have input config set to menu above will not work but you can override OnKeyPressed and similiar to react to input
obraz

1 Like

Hi im a beginner and i don’t know how to make this node
SmartSelect_٢٠٢٤٠٢٢٧_١٤٠٢٣٧_Free Adblocker Browser

If your ae planning on using it for main/pause menu or something similar I would suggest using the OnPreviewKeyDown function instead of OnKeyUp/Down, simply because it will allow you to use the key even if the focus is on it’s children, for this to happen you need to make sure that the widget itself is Focusable, after creating and adding the widget to the viewport you focus it, and if you want to go further and fix the mouse click outside of parent and child you will have to call Set Keyboar Focus on OnRemovedFromFocusPath Event. This works epic for me, and it’s less memory consuming then using On Event Tick because this will be triggered only when a key is pressed rather then each tick.



image
image