Change MIDI Messages created by MidiKeyboardComponent


#1

Hi Guys,

I have some problems for changing the behavior of MidiKeyboardComponent object, I am trying to handle myself creation of NoteOn and NoteOff messages from mousse activity.
I figure out I need to reimplement the functions “mouseDownOnKey” and “mouseUpOnKey” in the current component but I am beginner in OOP and I do not understand why the function “mouseDownOnKey” is never reached in this example adapted from the MainComponent.cpp file of the Handling Midi Event Tutorial.

class MainContentComponent  : public Component,
                          private MidiKeyboardStateListener
 {
public:
MainContentComponent() :  keyboardComponent( keyboardState, MidiKeyboardComponent::horizontalKeyboard)

{
    setOpaque (true);

    addAndMakeVisible (keyboardComponent);
    keyboardState.addListener (this);

    setSize (600, 400);
}

~MainContentComponent()
{
    keyboardState.removeListener (this);
}

void paint (Graphics& g) override
{
    g.fillAll (Colours::grey);
}

void resized() override
{
    keyboardComponent.setBounds (getLocalBounds().removeFromTop(80).reduced(8));
}

private:


bool mouseDownOnKey(int midiNoteNumber, MouseEvent & e)
{
    logMessage("My Own Note On");
    return false;
}
  
void logMessage (const String& m)
{
    Logger *log = Logger::getCurrentLogger();
    log->writeToLog(m);
}
 
void handleNoteOn (MidiKeyboardState*, int midiChannel, int midiNoteNumber, float velocity) override
{
    MidiMessage m (MidiMessage::noteOn (midiChannel, midiNoteNumber, velocity));
    postMessageToList (m);
}

void handleNoteOff (MidiKeyboardState*, int midiChannel, int midiNoteNumber, float /*velocity*/) override
{}

// This is used to dispach an incoming message to the message thread
class IncomingMessageCallback   : public CallbackMessage
{
public:
    IncomingMessageCallback (MainContentComponent* o, const MidiMessage& m)
       : owner (o), message (m)
    {}

    void messageCallback() override
    {
        if (owner != nullptr)
            owner->logMessage("Midi Message Received");
    }

    Component::SafePointer<MainContentComponent> owner;
    MidiMessage message;
};

void postMessageToList (const MidiMessage& message)
{
    (new IncomingMessageCallback (this, message))->post();
}

MidiKeyboardState keyboardState;
MidiKeyboardComponent keyboardComponent;

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContentComponent);
};

// (This function is called by the app startup code to create our main component)
Component* createMainContentComponent()     { return new MainContentComponent(); }