# Using MouseInputSource - they are all CONST

**URL:** <https://forum.juce.com/t/using-mouseinputsource-they-are-all-const/58986>\
**Category:** Audio Plugins\
**Created:** [November 29, 2023, 1:23pm UTC](https://forum.juce.com/t/using-mouseinputsource-they-are-all-const/58986 "2023-11-29T13:23:25Z")\
**Posts on this page:** 5\
**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:** [November 29, 2023, 1:23pm UTC](https://forum.juce.com/t/using-mouseinputsource-they-are-all-const/58986/1 "2023-11-29T13:23:25Z")

</div>

Hi, I am re opening a discussion I had a few years ago:

> [@Locking the mouse and making it invisible when using a slider?](https://forum.juce.com/t/locking-the-mouse-and-making-it-invisible-when-using-a-slider/44943):
>
> 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& …

I have looked over quite a few forum posts and haven’f found anything actually useful. According to everything I’ve seen, we should not be using setMouseCursor(), or juce:: Desktop::getInstance().setMousePostion() in favour of using the mouse input source classes. But there is just one problem. - MouseInputSource IS ALWAYS CONST. When MouseDown() gets called it gets a Const MouseEvent, same with mouseDrag and mosueUp. If you call any of the Desktop methods to get mouse sources such as getMainMouseSource() or getMouseSources() it gives you const MouseInputSources.  
Meaning I am not able to do what I want which is call source.hideCursor(), source.showCursor() and source.setScreenPosition(), wich I am not allowed to do when the MouseEvent or MouseInputSource is const.

All this being said it seems very obfuscated how to actually modify the mouse cursor using this better “modern” method. Can anyone shed some light?

---

<div class="post-metadata">

**Author:** ![nbacklund](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nbacklund/32/2969_2.png) [@nbacklund](https://forum.juce.com/u/nbacklund)\
**Post date:** [November 29, 2023, 9:24pm UTC](https://forum.juce.com/t/using-mouseinputsource-they-are-all-const/58986/2 "2023-11-29T21:24:04Z")

</div>

I have the following code in mouseDown() on my widgets (where e is the mouse event and fMousePosition is an instance variable in the widget class):

```
e.source.enableUnboundedMouseMovement (true);
fMousePosition = Desktop::getMousePosition();

```

And in mouseUp():

```
e.source.enableUnboundedMouseMovement (true);
Desktop::setMousePosition(fMousePosition);

```

Works like a charm for me!

---

<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:** [November 29, 2023, 10:23pm UTC](https://forum.juce.com/t/using-mouseinputsource-they-are-all-const/58986/3 "2023-11-29T22:23:06Z")

</div>

> [@nbacklund](#):
>
> `e.source.enableUnboundedMouseMovement (true);`

I think you meant to pass “false” here, right?

---

<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:** [November 29, 2023, 10:26pm UTC](https://forum.juce.com/t/using-mouseinputsource-they-are-all-const/58986/4 "2023-11-29T22:26:49Z")

</div>

I see, thanks, here is what worked for me if anyone else is stumbling across this:

```auto
class CustomSlider : public juce::Slider
{
public:
    CustomSlider() : juce::Slider() 
    {
        setTextBoxStyle(juce::Slider::NoTextBox, false, 0, 0);
        setSliderStyle(juce::Slider::RotaryVerticalDrag);
    }
    
    void mouseDown(const juce::MouseEvent &event) override
    {
        juce::Slider::mouseDown(event);
        setMouseCursor(juce::MouseCursor::NoCursor);
        event.source.enableUnboundedMouseMovement(true);
        mousePos = event.source.getScreenPosition();
    }
    
    void mouseUp(const juce::MouseEvent &event) override
    {
        juce::Slider::mouseUp(event);
        juce::MouseInputSource src = juce::MouseInputSource(event.source);
        src.enableUnboundedMouseMovement(false);
        src.setScreenPosition(mousePos);
        setMouseCursor(juce::MouseCursor::NormalCursor);
    }
private:
    juce::Point<float> mousePos;
};

```

---

<div class="post-metadata">

**Author:** ![nbacklund](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nbacklund/32/2969_2.png) [@nbacklund](https://forum.juce.com/u/nbacklund)\
**Post date:** [November 30, 2023, 11:17am UTC](https://forum.juce.com/t/using-mouseinputsource-they-are-all-const/58986/5 "2023-11-30T11:17:29Z")

</div>

> [@Toddler-Boy](#):
>
> I think you meant to pass “false” here, right?

Yeah, sorry!
