If the index sent into setMidiInput is 0, why does that mean the first(index+1) midi input is selected? Doesn’t a selected ID of 0 mean that nothing is selected?

MainContentComponent() : keyboardComponent (keyboardState, juce::MidiKeyboardComponent::horizontalKeyboard), 
startTime (juce::Time::getMillisecondCounterHiRes() * 0.001)
    {   
        addAndMakeVisible (keyboardComponent);
        keyboardState.addListener (this);

        addAndMakeVisible (midiInputList);
        midiInputList.setTextWhenNoChoicesAvailable ("No MIDI Inputs Enabled");
        auto midiInputs = juce::MidiInput::getAvailableDevices();
//return a list of MidiDeviceInfo Struct

        juce::StringArray midiInputNames;
        for (auto input : midiInputs)
            midiInputNames.add (input.name);
        midiInputList.addItemList (midiInputNames, 1);
//item ID will be its index in the StringArray + firstItemIdOffset.
        midiInputList.onChange = [this] { setMidiInput (midiInputList.getSelectedItemIndex()); };
 
        // find the first enabled device and use that by default
        for (auto input : midiInputs)
        {
            if (deviceManager.isMidiInputDeviceEnabled (input.identifier))
//MidiDeviceInfo Struct
//identifier : provided by the OS and it's format will differ on different systems e.g. on macOS it will be a number whereas on Windows it will be a long alphanumeric string
            {
                setMidiInput (midiInputs.indexOf (input));
                break;
            }
        }
 
        // if no enabled devices were found just use the first one in the list
        if (midiInputList.getSelectedId() == 0)
//A selected ID of 0 means that nothing is selected. All valid items in a combo box have non-zero IDs
            setMidiInput (0);
  }

   void setMidiInput (int index)
    {
        auto list = juce::MidiInput::getAvailableDevices();
 
        deviceManager.removeMidiInputDeviceCallback(list[lastInputIndex].identifier, this);
//Removes a listener that was previously registered with addMidiInputDeviceCallback()
        
        auto newInput = list[index];
 
        if (! deviceManager.isMidiInputDeviceEnabled (newInput.identifier))
            deviceManager.setMidiInputDeviceEnabled (newInput.identifier, true);
//Only devices which are enabled will have their events forwarded on to listeners.
 
        deviceManager.addMidiInputDeviceCallback (newInput.identifier, this);
        midiInputList.setSelectedId (index + 1, juce::dontSendNotification);
 
        lastInputIndex = index;
    }

Midi Input index values start at 0 (ie. 0 to the numMidiDevices-1) and combo box index values start at 1 (ie. 1 to the numberOfComboBoxValues). So you have to account for that when mapping between them.

comboBoxId = midiInputIndex + 1
midiInputIndex = comboBoxId -1

It might be a little opaque in the setup, but:

midiInputList.addItemList (midiInputNames, 1);

Per the docs for addItemList

The item ID of each item will be its index in the StringArray + firstItemIdOffset

Does that mean when nothing is selected, it will automatically select the first midi put

No, and actually I see what I said is incorrect. The part of about midi input index values is correct, but I mispoke about the combo box. A combobox has two way of addressing it’s items, by index and by id. Combobox index’ run from 0 to numItems - 1, while the id’s can be any, non-zero, arbitrary data you pass along to the additem function (or addItemList, in this case). When nothing is selected you will get a 0 for the Id and a -1 for the Index. This is all about setting up the combobox. Now when you use it, you have to take this knowledge into account. In this code we see

midiInputList.onChange = [this] { setMidiInput (midiInputList.getSelectedItemIndex()); };

Where onChange is called when a combobox item is selected, and this passes the index (0 to numItems-1), midiInputList.getSelectedItemIndex(), to the setMidiInput function, so it will receive a -1 if nothing is selected.

What about my original question?

I believe I have answered it, but if you do not understand then I have not done a good job. I believe you are getting confused by index and id. And I suspect you may be confused how one, or the other, is used to access either an entry in the midiInputs (an array of MidiInput’s) and midiInputList (a combobox with a list of midi input names). The midiInputs list uses an index of 0 - (numPorts-1) to access it’s items. A combobox has both an index and and id, the index is the same, 0 - (numPorts-1), and the ids are arbitrary numbers that can be assigned to each combobox entry, they can be 1 - maxIntValue. In the combobox, an index of -1, and/or an id of 0, indicated there is no selection.

But the index sent into setmidiinput is 0, not -1?

Either I don’t understand your question, or you are still not understanding my answer. I don’t know how to be any more clear. Hopefully someone else can come along and describe it in a way that makes sense to you.

Because that’s how the code is written. That comment above says explicitly that if nothing is selected (i.e., getSelectedId() returns 0), then we’ll just use the first item in the list, which is the item at index 0 (list[0] in the function setMidiInput()).

Note that that function then calls setSelectedId() with “index + 1”, which means it selects the item with 1 as its ID whenever nothing was selected. Whoever wrote this code simply wanted to never have nothing selected, apparently.