How to get mouse position when PRESSing button?

Hi,guys:
I want to get mouse position when pressing a button, there are three functions that I can use, GetMousePositionOnViewport, GetMousePositionOnPlatform, or GetMousePositionDPIScaled, none of them work.

Please help me if you have any solution, thank you so much!

Try GetPlayerController and then GetMousePosition

Please help me if you have any
solution, thank you so much!

Solution to what? All of them work, albeit in slightly different way. What results are you getting vs what are you expecting?

Thank you for your help, in fact, I have tried this for many times, when mouse is pressing, input mode will be switched to UI mode, so this function can not access mouse position any more, (0,0,0) will be returned.

Yes, they do return values, but I want to get values form 0 to viewport size, none of them returns expected value.
GetController()->GetMousePosition() returns currect value but it won’t change its value when performs a hold mouse movement.
GetMousePositionDPIScaled behaves the same.
GetMousePositionOnPlatform returns system mouse position which is not expected in my case.
GetMousePositionOnViewport returns value bigger than viewport size.

Maybe I haven’t had my question clear, so I comment here again:
those functions do return values, but I want to get values form (0,0) to viewport size, none of them returns expected value.

  • GetController()->GetMousePosition() returns currect value but it won’t change its value when performs a hold mouse movement.
  • GetMousePositionDPIScaled behaves the same.
  • GetMousePositionOnPlatform returns system mouse position which is not expected in my case.
  • GetMousePositionOnViewport returns value bigger than viewport size.

Glad to hear that, have a good day.

GetController()->GetMousePosition()
returns currect value but it won’t
change its value when performs a hold
mouse movement.

The above is not really true, this works fine:

And returns the correct viewport coordinates. Perhaps show us how you do it so we can poke holes in your method - it might help, since I sense some confusion here. :wink:

performs a hold mouse movement

What do you mean by that, for example? I assumed you meant hold mouse button down but I might be wrong here.


GetMousePositionDPIScaled behaves the same.

It does not behave the same. It uses scaled DPI whose curve you can set up in the Project Settings:

If you want a 1280x720 window to behave as if was a 1920x1080 one, you can - although this is a bit of a simplification at this point.


GetMousePositionOnPlatform returns
system mouse position which is not
expected in my case.

It does serve a specific purpose, true. Can be handy at times and you can use it like so:

Pixel Position would be your window coordinate. Viewport Position is the same as the Return Value.


GetMousePositionOnViewport returns
value bigger than viewport size

As expected. Think of it as window offset translation; and if I remember correctly it also uses DPI scaling as default.

3 Likes

From the other answer comment:

Thank you for your help, in fact, I
have tried this for many times, when
mouse is pressing, input mode will be
switched to UI mode, so this function
can not access mouse position any
more, (0,0,0) will be returned.

That’s normal. In UI mode, the Player Controller does not receive mouse input - that’s the whole point of using this mode. Perhaps you can tell us what you’re trying to do.

From what I gathered you want to obtain window mouse coordinates while in UI mode. You cannot use the PC for that; in UI Only mode, it’s the widget that handles cursor data at this point - take it from the widget. For example, overriding onMouseMove returns coordinates.

It is a similar situation to a typical Drag & Drop behaviour - the Player Controller has no clue where the cursor is when you drag items in the inventory.


What you’ve mentioned so far leads me to believe that by saying pressing button, you did not mean an input key at all but a widget button instead, in which case you can get your Pixel Position at any time like so:

This is player controller independent.

If in doubt, hit us up with a theoretical scenario that you’re trying to pull off.

1 Like

I am very grateful for your detailed answer, as you said, I want to get mouse position when mouse is pressing down and moving around, it is because of you that this world has become better.

Glad to hear you got it to work!

Oh, thank you for your reminding.

Thank you, You have saved my day.

Hi, I’m trying to reproduce this. It works perfectly with mouse click.
I would like to get a similar result with touch on Android instead of mouse. I get the coordinate 0.0.

Not sure why you’d need the above. Touch has its own handlers:

Thanks for the quick response.
Your solution is of course correct. My problem is that it works in the empty part of the widget, while I’m trying to make it work while clicking a button. The button however seems to completely absorb the input.

Essentially I’m trying to implement, without success so far, a swipe mode on the button, recording the start and end positions.

1 Like

Yeah, that’s the problem with the native button - or at least a problem with trying to make it do something it was never intended to do - they’re stubborn like this, and they consume input hungrily. Nice to have if you want a click / tap. But that’s it.

Make your own user widget button out of a border / image - this way you get to control everything. Also makes maintenance easier later on.

Thanks, I’ll try as you say