# How to move a widget panel when I press the mouse button, and follow the mouse cursor

**URL:** <https://forums.unrealengine.com/t/how-to-move-a-widget-panel-when-i-press-the-mouse-button-and-follow-the-mouse-cursor/469473>\
**Category:** UI\
**Tags:** unreal-engine, Widget, question, Blueprint\
**Created:** [August 9, 2020, 8:20pm UTC](https://forums.unrealengine.com/t/how-to-move-a-widget-panel-when-i-press-the-mouse-button-and-follow-the-mouse-cursor/469473 "2020-08-09T20:20:53Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Blackforcedragon](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/blackforcedragon/32/1010299_2.png) [@Blackforcedragon](https://forums.unrealengine.com/u/Blackforcedragon)\
**Post date:** [August 9, 2020, 8:20pm UTC](https://forums.unrealengine.com/t/how-to-move-a-widget-panel-when-i-press-the-mouse-button-and-follow-the-mouse-cursor/469473/1 "2020-08-09T20:20:53Z")

</div>

I try to move a widget when I press the mouse button down. It do move hut it can’t follow the mouse cursor and when I release the mouse button the widget will still move. the code is set up to stop moving the widget when the mouse button is released.

Can someone help me?

screenshot from codes  
alt text

 ![309669-0027.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/4/7/947a49a6707d7c63a3ceb5e909fe193e29708f47.png)

 ![309670-0028.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/0/8/408ec41d8d2c713aff138c3d55623be78ff93cd5.png)

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [August 9, 2020, 9:12pm UTC](https://forums.unrealengine.com/t/how-to-move-a-widget-panel-when-i-press-the-mouse-button-and-follow-the-mouse-cursor/469473/2 "2020-08-09T21:12:31Z")

</div>

Is it safe to assume this is for moving windows, like in, eh, Windows?

---

<div class="post-metadata">

**Author:** ![Blackforcedragon](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/blackforcedragon/32/1010299_2.png) [@Blackforcedragon](https://forums.unrealengine.com/u/Blackforcedragon)\
**Post date:** [August 9, 2020, 9:30pm UTC](https://forums.unrealengine.com/t/how-to-move-a-widget-panel-when-i-press-the-mouse-button-and-follow-the-mouse-cursor/469473/3 "2020-08-09T21:30:49Z")

</div>

Yes, that is what I try to do

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [August 9, 2020, 9:49pm UTC](https://forums.unrealengine.com/t/how-to-move-a-widget-panel-when-i-press-the-mouse-button-and-follow-the-mouse-cursor/469473/4 "2020-08-09T21:49:04Z")

</div>

Here’s the thing - we can’t have the player controller do anything here; widgets and the controller do not like talking to each other and often don’t know what the other is doing with the input.

* * *

Here are my wWindow’s _Up_ / _Down_ events for left mouse button:

 ![309682-onmousedown.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/8/4/584ab4caee9945e3158e927661ef67c26333a354.jpeg)

Above, the _Grab Offset_ is where we clicked in that window - it’s later on used when we move it around. When grabbed, the window calls an event dispatcher that is picked up by main canvas where we drag the windows.

Below, _onMouseUp_ calls the dispatcher, informing the main canvas that we no longer wish to drag the window.

 ![309683-onmouseup.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/3/2/93206862aecfc490a80b99eb0d8917e4cd17b3c9.jpeg)

* * *

Here’s my main canvas - wScreen - where the windows are added and the dragging happens:

 ![309672-screen.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/7/5/e75d6e78b67f2c71927b80fa885125de7cb33b77.jpeg)

The top bit creates a bunch of windows and registers the above-mentioned event dispatcher. That event tells the main canvas (wScreen) that a new windows needs dragging. A reference to that window is created and we give it higher Z so it stays on top of other windows.

We need to know where the cursor is in the main canvas as well. For this, I’m overriding _onMouseMove_:

 ![309684-onmousemove.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/f/4/1f499a1e9c34f0cef07c3e797a186c7d0568a0f6.jpeg)

Tick will move the window using its reference. The final location is calculated by subtracting the _Grab Offset_ from the _Local Screen Position_ produced by the override above.

[![Image from Gyazo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/b/1/db1ae84997e6f1cc067fc1f4a40c7de991dc3f05.gif)](https://gyazo.com/6a9b64f97a133d29e93e04176531bf4e)

---

<div class="post-metadata">

**Author:** ![Blackforcedragon](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/blackforcedragon/32/1010299_2.png) [@Blackforcedragon](https://forums.unrealengine.com/u/Blackforcedragon)\
**Post date:** [August 11, 2020, 1:43pm UTC](https://forums.unrealengine.com/t/how-to-move-a-widget-panel-when-i-press-the-mouse-button-and-follow-the-mouse-cursor/469473/5 "2020-08-11T13:43:42Z")

</div>

How do you get the “Call ED Grabbed window” node?

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [August 11, 2020, 1:49pm UTC](https://forums.unrealengine.com/t/how-to-move-a-widget-panel-when-i-press-the-mouse-button-and-follow-the-mouse-cursor/469473/6 "2020-08-11T13:49:04Z")

</div>

It’s an Event Dispatcher you create yourself and add inputs to.

---

<div class="post-metadata">

**Author:** ![Blackforcedragon](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/blackforcedragon/32/1010299_2.png) [@Blackforcedragon](https://forums.unrealengine.com/u/Blackforcedragon)\
**Post date:** [August 11, 2020, 4:38pm UTC](https://forums.unrealengine.com/t/how-to-move-a-widget-panel-when-i-press-the-mouse-button-and-follow-the-mouse-cursor/469473/7 "2020-08-11T16:38:11Z")

</div>

the widget to move, but it wont stop folowing the mouse cursor when I relses the left mouse button, Can you help me?

 ![309880-0034.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/9/6/596d83a2467fa0691a00b694645754fe9ae6c351.png)

 ![309891-0035.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/2/7/9278e2ab6ef1b9e53c4cfcf2b0b97087750a98a4.png)

 ![309892-0036.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/6/3/763413184c9850c7f4c5dd0b5393bd19087b2370.png)

 ![309893-0037.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/1/1/a110f185ba38b7f11da79b2f1dac2d313d3c6064.png)

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [August 11, 2020, 5:24pm UTC](https://forums.unrealengine.com/t/how-to-move-a-widget-panel-when-i-press-the-mouse-button-and-follow-the-mouse-cursor/469473/8 "2020-08-11T17:24:00Z")

</div>

Are you sure you’re not running some old code in the controller? The script above looks good.

---

<div class="post-metadata">

**Author:** ![Blackforcedragon](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/blackforcedragon/32/1010299_2.png) [@Blackforcedragon](https://forums.unrealengine.com/u/Blackforcedragon)\
**Post date:** [August 11, 2020, 5:27pm UTC](https://forums.unrealengine.com/t/how-to-move-a-widget-panel-when-i-press-the-mouse-button-and-follow-the-mouse-cursor/469473/9 "2020-08-11T17:27:07Z")

</div>

I made a new empty project to test this

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [August 11, 2020, 5:29pm UTC](https://forums.unrealengine.com/t/how-to-move-a-widget-panel-when-i-press-the-mouse-button-and-follow-the-mouse-cursor/469473/10 "2020-08-11T17:29:59Z")

</div>

Put a print string in the mouse up after the dispatcher. Does it print when you release the button?

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [August 11, 2020, 5:33pm UTC](https://forums.unrealengine.com/t/how-to-move-a-widget-panel-when-i-press-the-mouse-button-and-follow-the-mouse-cursor/469473/11 "2020-08-11T17:33:18Z")

</div>

Ok, I see it now. You have reversed _True_ | _False_ of _Unhandled_ | _Handled_ in the onMouseDown. It should be other way round - compare it with mine.

This means the _up_ can never execute since the widget does not know the key went down.

---

<div class="post-metadata">

**Author:** ![Blackforcedragon](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/blackforcedragon/32/1010299_2.png) [@Blackforcedragon](https://forums.unrealengine.com/u/Blackforcedragon)\
**Post date:** [August 11, 2020, 5:34pm UTC](https://forums.unrealengine.com/t/how-to-move-a-widget-panel-when-i-press-the-mouse-button-and-follow-the-mouse-cursor/469473/12 "2020-08-11T17:34:04Z")

</div>

It do it at random if I double click the button

---

<div class="post-metadata">

**Author:** ![Blackforcedragon](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/blackforcedragon/32/1010299_2.png) [@Blackforcedragon](https://forums.unrealengine.com/u/Blackforcedragon)\
**Post date:** [August 11, 2020, 5:38pm UTC](https://forums.unrealengine.com/t/how-to-move-a-widget-panel-when-i-press-the-mouse-button-and-follow-the-mouse-cursor/469473/13 "2020-08-11T17:38:14Z")

</div>

Thank you for the help, it works now.

---

<div class="post-metadata">

**Author:** ![Ruhlas](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Ruhlas](https://forums.unrealengine.com/u/Ruhlas)\
**Post date:** [February 14, 2023, 5:40pm UTC](https://forums.unrealengine.com/t/how-to-move-a-widget-panel-when-i-press-the-mouse-button-and-follow-the-mouse-cursor/469473/14 "2023-02-14T17:40:30Z")

</div>

1. Is there a reason to do the position setting in the container widget? I have done it directly in the MouseMove event of the window class. It doesnt seem to make any difference. The move event is for my case actually only picked up when hovering above some ui element of the container class anyways.
2. Also there seems to be a lag for the event tick (for dragging the window) and the actual mouse position on the screen. So if I drag quite fast then the mouse will leave the bounds of the window and the event is not going to be picked up. Is there a way to do this better? Preferably not having this “lag”.  
Maybe someone knowlageble still picks up this thread! Thanks in advance.

---

<div class="post-metadata">

**Author:** ![NeMontee](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/nemontee/32/766436_2.png) [@NeMontee](https://forums.unrealengine.com/u/NeMontee)\
**Post date:** [October 23, 2023, 6:32pm UTC](https://forums.unrealengine.com/t/how-to-move-a-widget-panel-when-i-press-the-mouse-button-and-follow-the-mouse-cursor/469473/16 "2023-10-23T18:32:56Z")

</div>

I added OnMouseButtonUp to canvas widget to clear DraggableWindow variable  
Seems to stop fine now  
Also did the same thing when mouse leaves canvas widget

---

<div class="post-metadata">

**Author:** ![tootzoe1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@tootzoe1](https://forums.unrealengine.com/u/tootzoe1)\
**Post date:** [December 29, 2024, 2:16pm UTC](https://forums.unrealengine.com/t/how-to-move-a-widget-panel-when-i-press-the-mouse-button-and-follow-the-mouse-cursor/469473/17 "2024-12-29T14:16:43Z")

</div>

This is my another way to add widgets to viewport directly, instead of adding sub-widgets to a main root widget…

 ![Screenshot 2024-12-29 205415](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/4/e/c4e02e29055ab8d0b6440a719d059ea73c1509da.png)

 ![Screenshot 2024-12-29 205444](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/6/3/163471d38d1b2fb1f6c900addef926ac7a84361b.png)

![399165846-d48726a7-4289-439c-893a-8dc6fcf2bb0b (1)](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/9/3/e93b3b2cf3f3f1a82c612c87d011cdedaccd428f.gif)

demo can be found here:

> **[GitHub - tootzoe/BycTutorMiscAll: BycTutorMiscAll](https://github.com/tootzoe/BycTutorMiscAll)**
>
> BycTutorMiscAll
