Control widget with key press?

Hi,

I’m sure this should be really simple, but I can’t figure out exactly how it’s done.

I have a UI I have built in a Widget Blueprint. I want to be able to make changes to it with key presses, for example pressing Space starts/stops something on the UI.

But in the Widget Blueprint itself, I can’t get a key press event. It seems one can only get those types of events inside a Player Controller Blueprint, or am I mistaken?

So I assume I need the key press to fire some kind of event in the Player Controller, which the Widget is listening for?

Would love some help to figure this out!

2 Likes

Hi localstarlight,

You need to use “Set Keyboard Focus” after “Add to Viewport”

Then go to your Widget Blueprint and override the function “On Key Down” or “On Key Up”, whatever you want.

By last, in the new function you can detect any key you want.

I really don’t know all the differences about return Handled or Unhandled, but I know that Handled block another Inputs that are in another blueprint, Unhandled not.

6 Likes

Thanks for your reply (ages ago!) – I still haven’t managed to make this work.

I’ve tried what you suggested (I think). Here is my Level Blueprint:

And here’s my Widget Blueprint:

When I press ‘A’ it doesn’t do anything – it doesn’t go down either fork of the branch, so it’s clearly not coming through.

What am I doing wrong?!

1 Like

So I realised I hadn’t done what you’d suggested. For some reason the ‘Set Keyboard Focus’ node wasn’t showing up for me until I turned off ‘Context Sensitive’.

So now it looks like this:

But it still doesn’t do anything when I press the ‘A’ key…

Not 100% certain, but if that’s in the Level Blueprint, then you will probably need to tell who the Owning Player is when creating the widget.

I have tried adding that, so now Level Blueprint looks like this:

But still doesn’t work…

So, does it actually draw the widget to the screen, or is it just not accepting the override input?

The widget draws fine, just not accepting any input. Is there another step/setting needed to make this work?

I can’t believe this is such an unusual thing to want to do, and yet nothing I try seems to work.

Does anyone know of a tutorial that goes through such a thing? Or does anyone have a working example of affecting a widget with the keyboard that I could look at?

https://answers.unrealengine.com/questions/166028/keyboard-input-in-userwidget.html

this works, however you will need to use event tick for all the widget that needs input, and you need to define it for each key.
i am in the same page as you trying to enable the input for the widget, even worst since i want to swipe my widget.

There’s one piece missing here that I needed to make this work. Maybe it’s new since this was posted. In the widget, after event construct, you need to set Is Focusable to true. This allows the Set Keyboard Focus node to work. Then the key down event overrides also work.

Make an input action for your key press.

From inside the Widget Blueprint:
EventOnInitialize->SetIsFocusable
EventOnMouseEnter->SetKeyboardFocus
EventOnMouseLeave->SetFocusToGameViewport

Works for me, thanks guys. Unreal 4.23

Maybe I’m too late but the thing you need to do to make a widget get some input is to use SetInputModeUIOnly when you create your widget. So all the input events will be sent to the widget

2 Likes

Very very (necro tier) late, but I found a great method to make it work and I feel it’s worth sharing since it doesn’t use Event Tick.
(images included as a numbered album, since I’m a new user)

First step is allowing the Widget to be focusable, by:

  1. Appending ‘Set Keyboard Focus’ after ‘Add To Viewport’
    Image 1

  2. Enabling ‘Is Focusable’ in Widget designer
    Image 2

Next up is the override of desired function (KeyPressed/Released or any other)

  1. Create an Event Dispatcher. Mine closes the pause menu, so I’ll call it appropriately
    image 3

  2. Drag the dispatcher onto the function and select Call.
    image 4

  3. In the function use Get Key and a set of Equals (Key) to search for keys you need. Every additional key you wish to include requires an additional pair of Equals (Key) and Branch nodes. Trues must go into the Call dispatcher, where as the final false must skip it to the Return Node. Here’s an example:
    image 5

Now the final step is incorporating that into your widget’s main Event Graph.

  1. To begin you need to bind an event there to the new dispatcher, so drag the dispatcher and choose Assign.
    image 6

  2. Plug Event Pre Construct or anything that happens before the dispatcher can be used, like so:
    image 7

  3. Lastly add any logic for what happens once the event is triggered, from the event created by Assign.

2 Likes

Thank you. It helps for me.

I read this and I see some people go in tick solution, I think best way is to use custom action. In my case On Key Down:

Then custom event:

Widget must be set as focusable:
image

And you not neee to set on widget create focus, it can be done in widget on pre construct stage:
image

1 Like