Using MouseInputSource - they are all CONST

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

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?

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!

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

1 Like

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

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;
};

Yeah, sorry!