Midi usb problem on Leonardo with encoder ky040

The value of the encoder changes if you turn the encoder.
You are asking

You have to specify what "asking the encoder" means.

I'm sure that your code should react on any rotating of the encoder at any time.
So the code must permanently = always check did the encoder rotate?

Does your MIDI-Software make a request to the arduino-Leonardo?
This would be some kind of "asking"
But I guess this is not what you mean.

If english is not your native language write down what you want to ask in your native language and let do google-translate the translation.

Simply describe in more detail how you want the code to react.
You turn the encoder - and then?
what shall happen?

by the way
please RE-edit your posting to present your code as a code-section

click on the pencil-icon below your posting and then re-edit your posting following this short tutorial

best regards Stefan