Stopping an SvgSwitch from switching on-click?

Hello!

I have a custom button that sub-classes SvgSwitch. When a user shift-clicks on the button, I don’t want the button’s value to change. My code currently looks like this:

    void onButton(const event::Button &e) override
    {
        if (e.button == GLFW_MOUSE_BUTTON_LEFT && e.action == GLFW_PRESS)
        {
            e.consume(this);

            if (module->shift_pressed)
            {
               // stop the button state from toggling here?
            }
        }
    }

Thanks!

You can use e.mods for this:

if (e.button == GLFW_MOUSE_BUTTON_LEFT && (e.mods & RACK_MOD_MASK) ==GLFW_MOD_SHIFT)
{
 do stuff
}

Ah, sorry, yes indeed – I understand how to check for the shift key. What I don’t understand is how to stop the button from changing state when it’s clicked.

:pray:

Ah, right. I’m not sure there’s a simple way to do that. You might need to add a custom onDragStart and pop your logic in there. Have a look at Switch::onDragStart which checks for GLFW_MOUSE_BUTTON_LEFT only. Adding e.mod logic might do what you need.

It’s slightly off topic but how do I check if that Shift-LeftMouseClick was on params[TEST_PARAM] knob?

What I ended up doing was this:

In my main module struct, I created a bool to track the shift-key state:

struct GrooveBox : VoxglitchSamplerModule
{
  bool shift_key = false;

Then in my main module’s widget code, I tested for the shift key in the onHover method:

  void onHoverKey(const event::HoverKey &e) override
  {
    GrooveBox *module = dynamic_cast<GrooveBox *>(this->module);
    assert(module);

    module->shift_key = ((e.mods & RACK_MOD_MASK) & GLFW_MOD_SHIFT);
    module->control_key = ((e.mods & RACK_MOD_MASK) & RACK_MOD_CTRL);

    ModuleWidget::onHoverKey(e);
  }

Then, I can test for the shift or control key in my widgets as long as the widgets have a pointer to the module. For example:

    // In some widget code...
    void onDragMove(const event::DragMove &e) override
    {
        if (module && module->shift_key)
        {

OH… in your case, when you read the knob, you can test for the shift_key.

I’m starting to move away from keyboard input though. (At least, as much as possible.) Rack itself uses keyboard commands, and overriding rack’s features sometimes doesn’t work, and when it does, it can mess with the user’s intended actions. In my humble opinion. I also ran into some trouble with Shift+Control working on Windows and not Mac.

Hope this helps!!