[Tutorial] How to control UMG widget that uses mouse cursor with both mouse and gamepad

Working on my current game project I’ve came to the point where I needed to make UMG user interface which can be controlled both by mouse and gamepad. There were certain obstacles while I was trying to make this system work. Now I’m ready to post this little tutorial with the example of working result. Please note that the tutorial is based only on Blueprints as my project is 100% Blueprints-based and I have no experience of working with C++ directly.

This is how my game moves mouse cursor with gamepad stick. The idea is to use GetGamepadLeftThumbstickX-Axis and GetGamepadLeftThumbstickY-Axis, round them, multiply the results by desired sensitivity, and set coordinates for SetMousePosition node with restrictions tied to viewport size. The entire script needs to be put into player controller of your game project. Screenshot below shows script that works perfectly in my game (the PressAnyKey part in the script is not tied to the cursor movement, but may also be handy, so I decided to capture the entire PlayerController script from my game):

DetectStickMovement macro is needed to determine if the stick is actively moving to avoid interference with mouse movement when the stick is inert:

I also use complicated system that allows game’s UI to be controlled by hotkeys and gamepad keys. But this will require much more time to describe. I’ll do this when I’ll have some free time.

Hope this tutorial will help those, who also want to use gamepad in their games this way :slight_smile:


Thank you! Thats work very well in UE5.02 since the Virtual Cursor plugin isn’t updated yet.

The only think I noticed is that if I start pressing the joystick in the X axis it not register changes in the Y axis until I release the joystick back to the neutral positicon. And it also happens in reverse: if I start moving it in the Y axis it no register any X axis movement until I release the joystick. Is a minor issue for me because I use it for menus, but I assume it will be more severe for others.

Anyway, thanks ^^

1 Like

You are welcome!

I’ll try to reproduce issue you mentioned and think what can be done with it when I’ll have time. Thanks for the feedback! :slight_smile:

1 Like

Hi, do you have an example of pressing button A from a gamepad

copiado tal cual y no funciono

@solokapa, you probably made a mistake in your script. Or, if you are trying to use PS4 gamepad on PC, then you need DS4Tool in order to make your PC detect attached gamepad.

Hi! :slight_smile: As far as I remember, “A” button on XBox gamepad is marked as Gamepad Face Button Down in the engine. So, you need to choose this button in the node that detects pressed button, and connect the subsequent branch with the next node with True pin instead of False pin.

Beautiful work. Thank you for sharing.

1 Like

look cool can someone show it working in a video please

1 Like

How do I press a button from the gamepad? Since the button detects when they click, but to do it with gamepad I do not know the way to simulate the same action.

It’s not the greatest solution, and is quite tedious depending on how much you have in your HUD, but this works for me at least. I found I needed to overlay my non-btn UMG elements that had click events with a transparent btn, that way you can use the OnHovered and Unhovered events to set a boolean. Then you can simple collapse all your click logic into a function, and call that from your pawn/character’s BP

1 Like