@Rognvald You can put it all in the interface MIDI processor if you want to keep things simple - just avoid doing any UI stuff in the MIDI callbacks.
// This sort of thing should be avoided in a non-deferred script.
Buttons[index].setValue(true);
Buttons[index].changed();
However the ideal situation is you separate UI logic from MIDI processing by using separate processors for individual tasks.
You can add the velocity code I described above alongside your current script.
Those reg variables you have should most likely be const. Rule of thumb is if the values are fixed (or it's an array or object) use a const. If the value is going to be dynamic then use a reg.