[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: