Strange issue with MidiKeyboardComponent and buttons

while I hold down one or more keys on the pc keyboard, if I press with any mouse button a control belonging to a group or panel (same parent) that has a button added, the notes are sent again.

At first I could not find the problem, then I realized that by removing the button from the panel the problem disappeared. So I tried adding buttons to other panels (just addAndMakeVisible), and sure enough, all the “sibling” controls get a strange connection to the keyboard that makes the pressed keys replay, even chords.

I have tried to trace, but it is very difficult. Does anyone know what this could be?

I tried updating the AudioPluginDemo example project to include a TextButton in the same parent component as the MidiKeyboardComponent. When I hold down some keys on the computer keyboard, and then click one of the Slider components, the MidiKeyboardComponent loses focus and any held notes are ended (a noteOff is sent for each). I don’t see any re-triggered notes (I stuck a breakpoint in MidiKeyboardState::noteOn to make sure). This seems to be the case whether or not the TextButton component is present.

From what I’ve seen, the MidiKeyboardComponent appears to be behaving as expected. In order to investigate this further, we’ll need more details so that we can reproduce and debug the problem ourselves. Please can you provide some more details about the exact steps you’re taking to trigger the issue? It would be helpful if you could either try to reproduce the problem in one of the example projects included in the JUCE repository, or provide a link to your project, so that we can be sure we’re testing exactly the same scenario.

Well, I started a new plugin project and added a keyboard and a button to it, and really, clicking on the button loses the keyboard focus.

At that moment I remembered that at the beginning of the project I encountered this problem, and I did not want to lose focus. I wanted to be able to move the sliders while playing notes on the pc keyboard. So I tried it with grabKeyboardFocus. But I couldn’t include it anywhere since an assertion was produced because it had to be created and visible, except in the paint method.

After including keyboardComponent.grabKeyboardFocus () in the paint method of AudioProcessorEditor I saw that it worked fine, I could move the sliders while playing the keys without any problem, I have even been able to create my graphical controls with opengl and no problem. Until I have included a button in the same panel.

So I already know where the problem is although I do not understand why it happens with add buttons. And I just look for the solution. What would be the correct procedure to keep the keyboard in focus without interference?

You could take a look at setWantsKeyboardFocus and setMouseClickGrabsKeyboardFocus. With these functions you should be able to prevent other components from stealing keyboard focus.

I’m still testing the clean project with keyboard and button, but I can’t keep focus. From what I’ve seen in this example, it should grab the initial focus with a timer, although I guess it shouldn’t be called continuously, and I should use the other functions.

I have tried from my editor’s constructor with:

 button.setMouseClickGrabsKeyboardFocus (true);
 button.setWantsKeyboardFocus (true);

I’m missing something, although I think I’m closer to solving it with your help

The problem is that your buttons/sliders are stealing focus when you click them. To fix the issue you’ll need to set your buttons and sliders to not want keyboard focus. So, you need to pass false instead of true to thos functions, and you’ll also need to call those same functions on all of the controls that you want to interact with while using the keyboard.

1 Like

Well, everything is resolved then :beers:

This is biting me hard at the moment. I have dozens of Components all over my multi-tabbed UI and is there really no other way to have the MidiKeyboardComponent keep focus than going through every single Component in my UI and doing a setWantsKeyboardFocus(false)?

There really should be something higher level for this.

Has anyone found a better solution?