Detecting Modifier Key when touching Slider/RotaryKnob

I need to be able to do something special in “sliderValueChanged” when touching, that is clicking or adjusting a slider (actually a rotary knob), and at the same time either holding down the “Ctrl” key or right-clicking the mouse button. I need to do this, on many rotary knobs, and each action although related, is unique to each knob.

I had hoped I could simply add some modifier key detection code inside my below “sliderValueChanged”.

   void MutineerAudioProcessorEditor::sliderValueChanged (Slider* slider)
    {
    	String id = slider->getComponentID ();

        // Detect modifier key here


    }

There is a static variable ModifierKeys::currentModifiers that you can check at any time:

if (juce::ModifierKeys::currentModifiers.isCtrlDown())
    // ...
1 Like

Thanks a bunch! What I am doing is instead of a large LFO matrix window, I want to be able to set/unset a knob (parameter) as an LFO destination with a special click.

I see, in that case I would probably rather override mouseDown of the slider, reason being that you can act without needing to change the slider value. Just don’t forget to call the mouseDown of the super class…

1 Like

Sounds reasonable, but I am not sure how to override mouseDown of the slider. On another related subject, I just remembered that Ctrl is used to fine adjust sliders, but not rotary knobs which I could really find helpful in my synth.

Ok I think I got it.

I already had; void mouseDown (const MouseEvent& event) override;

So I add a mouse Listener to my rotary knob.

Then I test for modifier key in mouseDown on the specific knob;

void MutineerAudioProcessorEditor::mouseDown (const MouseEvent& event)
{
	if (event.eventComponent == &myKnob)
      {
        if (juce::ModifierKeys::currentModifiers.isCtrlDown ()) setLFOTarget(&myKnob);
      }
}

And then of course in sliderValueChanged I also need to check if the same modifier key was pressed so to avoid changing the slider value.

There’s a better way:

class MySlider : public juce::Slider
{
public:
    void mouseDown (const juce::MouseEvent& event) override
    {
        if (juce::ModifierKeys::currentModifiers.isCtrlDown())
            toggleYourLfo()
        else
            juce::Slider::mouseDown (event); 
    }
private:
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MySlider)
};

This way the mouseDown will not reach the slider, if ctrl was pressed… I think the subsequent mouseDrag should have no effect then… haven’t tested it

1 Like

Ok thanks a bunch I will test it within the next few minutes :wink:

Ok my knowledge on C++ is clearly lacking here. I am confused about where to put the code you presented. If I put it in a “MySlider.h” file, and then in my PluginEditor.h include “MySlider.h” and then in same file do MySlider mySlider, I get a compiler error “no appropriate default constructor available”

I imagine I could just put the code in PluginEditor.h somewhere, where I also have my custom lookandfeel class, so it would work with all my knobs from any component file.

Ok, I left that out for brevity.
But I can add it here:

class MySlider : public juce::Slider
{
public:
    MySlider() = default;

    MySlider (const juce::String &componentName) : juce::Slider (componentName) {}

    MySlider (juce::Slider::SliderStyle style, juce::Slider::TextEntryBoxPosition textBoxPosition) 
    : juce::Slider (style, textBoxPosition) {}

    void mouseDown (const juce::MouseEvent& event) override
    {
        if (juce::ModifierKeys::currentModifiers.isCtrlDown())
            toggleYourLfo()
        else
            juce::Slider::mouseDown (event); 
    }
private:
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MySlider)
};

Hope that helps

1 Like

Ok thanks I’ll give that a try.

Seems to be working like a charm and you’re right holding down Ctrl while dragging the slider(knob) does not trigger sliderValueChanged.

You’re a life, well program saver, thanks!

1 Like