# Locking the mouse and making it invisible when using a slider?

**URL:** <https://forum.juce.com/t/locking-the-mouse-and-making-it-invisible-when-using-a-slider/44943>\
**Category:** General JUCE discussion\
**Created:** [March 11, 2021, 12:16pm UTC](https://forum.juce.com/t/locking-the-mouse-and-making-it-invisible-when-using-a-slider/44943 "2021-03-11T12:16:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dariop](https://avatars.discourse-cdn.com/v4/letter/d/4491bb/32.png) [@dariop](https://forum.juce.com/u/dariop)\
**Post date:** [March 11, 2021, 12:16pm UTC](https://forum.juce.com/t/locking-the-mouse-and-making-it-invisible-when-using-a-slider/44943/1 "2021-03-11T12:16:18Z")

</div>

Hi, in my plugin I have quite a lot of slider, but I want the slider the mouse to disappear and stay in the exact same position as I’m interacting with the slider, like how Ableton does it. However I have not been able to find a suitable function/method that fits my needs. The closest thing I have is doing this:

```
void MySlider::mouseDown(const juce::MouseEvent& m)
{
    juce::Slider::mouseDown(m);

    m.source.enableUnboundedMouseMovement(true);
}

void MySlider::mouseUp(const juce::MouseEvent& m)
{
    juce::Slider::mouseUp(m);

    m.source.enableUnboundedMouseMovement(false);
}

```

However this does not achieve the behaviour I am after. The mouse does disappear but it moves slightly after I have finished interacting with the slider. I want it to return to the exact same position that it started at.

Thanks

---

<div class="post-metadata">

**Author:** ![baramgb](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/baramgb/32/12822_2.png) [@baramgb](https://forum.juce.com/u/baramgb)\
**Post date:** [March 11, 2021, 2:25pm UTC](https://forum.juce.com/t/locking-the-mouse-and-making-it-invisible-when-using-a-slider/44943/2 "2021-03-11T14:25:10Z")

</div>

In the MouseDown Callback you could save the position (`savedPoint = juce::getMousePosition()`) and in the MouseUp do `juce::setMousePosition(savedPoint)`.

But I suggest to test this on all OSes. There could be diffenrent behavior.

---

<div class="post-metadata">

**Author:** ![dariop](https://avatars.discourse-cdn.com/v4/letter/d/4491bb/32.png) [@dariop](https://forum.juce.com/u/dariop)\
**Post date:** [March 11, 2021, 8:34pm UTC](https://forum.juce.com/t/locking-the-mouse-and-making-it-invisible-when-using-a-slider/44943/3 "2021-03-11T20:34:58Z")

</div>

thanks, I’ll give it a try. I didn’t know these methods existed

---

<div class="post-metadata">

**Author:** ![Toddler-Boy](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/toddler-boy/32/2007_2.png) [@Toddler-Boy](https://forum.juce.com/u/Toddler-Boy)\
**Post date:** [March 12, 2021, 7:47am UTC](https://forum.juce.com/t/locking-the-mouse-and-making-it-invisible-when-using-a-slider/44943/4 "2021-03-12T07:47:00Z")

</div>

Is there a way for the mouse-cursor to stay frozen, but the mouse-movement still changes the value up/down?

---

<div class="post-metadata">

**Author:** ![baramgb](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/baramgb/32/12822_2.png) [@baramgb](https://forum.juce.com/u/baramgb)\
**Post date:** [March 12, 2021, 8:09am UTC](https://forum.juce.com/t/locking-the-mouse-and-making-it-invisible-when-using-a-slider/44943/5 "2021-03-12T08:09:42Z")

</div>

Sorry, it is `juce::Desktop::setMousePos()`

---

<div class="post-metadata">

**Author:** ![baramgb](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/baramgb/32/12822_2.png) [@baramgb](https://forum.juce.com/u/baramgb)\
**Post date:** [March 12, 2021, 8:13am UTC](https://forum.juce.com/t/locking-the-mouse-and-making-it-invisible-when-using-a-slider/44943/6 "2021-03-12T08:13:41Z")

</div>

Actually therefore `event. source.enableUnboundedMouseMovement(true)` should work.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [March 12, 2021, 8:20am UTC](https://forum.juce.com/t/locking-the-mouse-and-making-it-invisible-when-using-a-slider/44943/7 "2021-03-12T08:20:37Z")

</div>

It would be better to use the methods of MouseInputSource. You get the MouseInputSource from the event, but the event is const. You will have to do an ugly workaround. const\_cast is not adviseable, so what I do is find the non-const version of the event.source and work with that:

```auto
for (auto& source : juce::Desktop::getInstance().getMouseSources())
{
    if (source == event.source)
    {
        source.setPosition (..);
        // ...
        break;
    }
}

```

Would be nice to have a way to get this from the event directly…

---

<div class="post-metadata">

**Author:** ![dariop](https://avatars.discourse-cdn.com/v4/letter/d/4491bb/32.png) [@dariop](https://forum.juce.com/u/dariop)\
**Post date:** [March 12, 2021, 9:54am UTC](https://forum.juce.com/t/locking-the-mouse-and-making-it-invisible-when-using-a-slider/44943/8 "2021-03-12T09:54:54Z")

</div>

enableUnboundedMouseMovement() does not work unfortunately, the mouse does not return to the same position. It shifts slightly.

---

<div class="post-metadata">

**Author:** ![yfede](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yfede/32/506_2.png) [@yfede](https://forum.juce.com/u/yfede)\
**Post date:** [March 15, 2021, 1:38pm UTC](https://forum.juce.com/t/locking-the-mouse-and-making-it-invisible-when-using-a-slider/44943/9 "2021-03-15T13:38:55Z")

</div>

> [@dariop](#):
>
> It shifts slightly.

Yes, IIRC the cursor position is clipped back within the Slider’s bounds from where it would have been dragged if it was visible, so it won’t be too distant from where the mouseDown happened, but no further effort is made to bring it back exactly there.
