Keyboard Focus Stick to one Component?


I am new to JUCE and am developing an audio app that basically functions as a midi synth. I have the MidiKeyboardComponent showing at the bottom of the window all the time and I want it to have keyboard focus all the time. The problem I am running into right now is that when I click on other components (i.e. I have a OSC component and I can drag sliders within it), the keyboard focus on MidiKeyboardComponent is gone. I have to click somewhere on the keyboard with my mouse to “re-activate” the focus. Is there a function provided that allows me to “lock” the keyboard focus to a component forever?

You need to call setWantsKeyboardFocus (false) on all other components that can grab focus. This way the focus will not be moved away from your keyboard component. Note that normally the components are not interested in keyboard focus, so you only need to disable the focus for the components that can grab it. You can have a look for setWantsKeyboardFocus (true) in JUCE codebase, to see a small list of components that by default can grab focus (such as a button or a text editor). You need to disable wants focus flag for them + for any other components for which you have set that flag to true yourself. When in doubt, you can place a breakpoint in void MidiKeyboardComponent::focusLost to see which component steals the focus from the MIDI keyboard.